我需要使用awk形成一个变量,这个sintaxis [Subrack = xxx,Slot = x]:[sample]。如何使用awk从下面的输出中仅获取每个CPU的最新样本?
Time Object The_average_processor_load_of_a_board_in_percent
22:30 Subrack=MAIN,Slot=1 1
22:30 Subrack=MAIN,Slot=12 21
22:30 Subrack=MSE1,Slot=1 1
22:30 Subrack=MSE1,Slot=10 3
22:30 Subrack=MSE1,Slot=11 3
22:30 Subrack=MSE2,Slot=1 1
22:30 Subrack=MSE2,Slot=10 1
22:45 Subrack=MAIN,Slot=1 1
22:45 Subrack=MAIN,Slot=12 20
22:45 Subrack=MSE1,Slot=1 1
22:45 Subrack=MSE1,Slot=10 3
22:30 Subrack=MSE1,Slot=11 2
22:45 Subrack=MSE2,Slot=1 1
22:45 Subrack=MSE2,Slot=10 1
我尝试了以下代码
awk '!seen[$0]++ {lines[i++]=$0} END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}' {print} {++seen[$0]}'
没有获得理想的结果。
已发布的输出被剪切,完整的输出可以在此处查看:https://www.dropbox.com/s/4d45x6m1701lpvz/complete_output.txt?dl=0
答案 0 :(得分:1)
使用数组来保存每个CPU的时间和样本。如果当前行的时间高于该CPU的保存时间,则替换它们。
awk '$0 > time[$2] { time[$2] = $1; sample[$2] = $3; }
END { for (cpu in sample) printf("[%s]:[%d]\n", cpu, sample[cpu]) }'