我有一个 shell 脚本 (zsh),它输出这个(数据库计数结果集),并带有一些分散在各处的常规文本:
Regular lines of output that should be ignored.
+-----------------------------------------+
| rowcount.account_config records deleted |
+-----------------------------------------+
| 2 |
+-----------------------------------------+
+----------------------------------------+
| rowcount.accounts_user records deleted |
+----------------------------------------+
| 2 |
+----------------------------------------+
我想将其转换为这种格式的文本:
Regular lines of output that should be ignored.
- account_config records deleted: 2
- accounts_user records deleted: 2
到目前为止,我已经关闭的是:
functionThatOutputsText | awk -F '|' '/rowcount\./ {print "- " $2 ": " $NF}'
哪个是不对的:
- rowcount.account_config records deleted :
- rowcount.accounts_user records deleted :
由于只有键和值包含在一对
|
中,因此您可以使用计数器来在存储键和使用最后一个键输出值之间进行选择:
awk '-F *\| *' '
NF == 3 {
if (++n % 2)
key = $2
else
print key ": " $2
next
}
!/^\+-*\+$/
'