用于处理脚本输出的 AWK 命令

问题描述 投票:0回答:1

我有一个 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 : 
shell awk text zsh
1个回答
0
投票

由于只有键和值包含在一对

|
中,因此您可以使用计数器来在存储键和使用最后一个键输出值之间进行选择:

awk '-F *\| *' '
NF == 3 {
  if (++n % 2)
    key = $2
  else
    print key ": " $2
  next
}
!/^\+-*\+$/
'

演示:https://awk.js.org/?snippet=MzEqEr

© www.soinside.com 2019 - 2024. All rights reserved.