重新排序并重新编号命令列表

时间:2019-05-11 08:35:07

标签: awk

我正在尝试对以下命令块重新排序并重新编号,以便每个命令都遵循遵循如下结构的编号:

0.1
0.2
..
0.9
1.0
1.1
..

到目前为止,我已经可以使用以下命令将数字插入行中,但是不确定执行重新排序和重新编号的最佳方法是什么。我可以在这方面寻求帮助吗?

awk '$2 ~ /\y[0-9]+\y/' eem.txt

需要过滤的输出

 event manager applet monitorHealth authorization bypass
    event manager applet monitorHealth
    event syslog pattern xx maxrun 50 ratelimit 50
    action 0.1 cli command xx
    action 0.2 cli command xx
    action 0.4 cli command xx
    action 0.4 cli command xx
    action 0.4 cli command "undebug all”
    action 0.3 cli command xx
    action 0.4 cli command xx
    action 0.5 cli command xx
    action 0.6 cli command xx
    action 0.7 cli command xx
    action 0.8 cli command xx
    action 0.9 cli command xx
    action 1.1 cli command xx
    action 1.2 cli command xx
    action 1.3 cli command xx
    action 1.4 cli command xx
    action 1.5 cli command xx
    action 1.6 cli command xx
    action 1.7 cli command xx
    action 1.8 cli command xx
    action 1.9 cli command xx
    action 2.1 cli command xx
    action 2.2 cli command xx
    action 2.3 cli command xx

谢谢。

1 个答案:

答案 0 :(得分:1)

要重新编号行,只需为新编号添加一个计数器,然后为每条匹配的行替换第二列的当前值,并增加0.1。

$ awk -v counter=0.1 '$2 ~ /^[0-9]+\.[0-9]$/ { $1 = "    " $1; # Keep the leading spaces
                                               $2 = sprintf("%.1f", counter);
                                               counter += 0.1 } 1' eem.txt
event manager applet monitorHealth authorization bypass
    event manager applet monitorHealth
    event syslog pattern xx maxrun 50 ratelimit 50
    action 0.1 cli command xx
    action 0.2 cli command xx
    action 0.3 cli command xx
    action 0.4 cli command xx
    action 0.5 cli command "undebug all”
    action 0.6 cli command xx
    action 0.7 cli command xx
    action 0.8 cli command xx
    action 0.9 cli command xx
    action 1.0 cli command xx
    action 1.1 cli command xx
    action 1.2 cli command xx
    action 1.3 cli command xx
    action 1.4 cli command xx
    action 1.5 cli command xx
    action 1.6 cli command xx
    action 1.7 cli command xx
    action 1.8 cli command xx
    action 1.9 cli command xx
    action 2.0 cli command xx
    action 2.1 cli command xx
    action 2.2 cli command xx
    action 2.3 cli command xx
    action 2.4 cli command xx