协助awk/bash捕获内存差异 [英] Assistance with awk/bash to capture memory difference
本文介绍了协助awk/bash捕获内存差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从以下文件中提取以下输出:
I am trying to extract the following output from the following file:
xr_lab# show clock
Thu Sep 19 14:38:02.812 WIB
14:38:02.893 WIB Thu Sep 19 2019
xr_lab#
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:41:08.084 WIB
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
6777 ospf 24292985 24293753 768 272634
7582 mibd_interface 8670334 8484152 -186182 267657
xr_lab#show clock
Thu Sep 19 14:42:42.425 WIB
14:42:42.497 WIB Thu Sep 19 2019
xr_lab#
xr_lab#
xr_lab#show memory compare report
Thu Sep 19 14:45:42.091 WIB
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
6777 ospf 24294569 24283592 -10977 227389
7582 mibd_interface 8369050 8514825 145775 126259
期望的输出
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8670334 8484152 -186182 267657
7582 mibd_interface 8369050 8514825 145775 126259
7582 mibd_interface 8446906 8264885 -182021 322280
7582 mibd_interface 8264884 8264960 76 284409
-------------------------------------------------------------------------------
-222352 <time difference>
这是我到目前为止所拥有的.
Here is what I have so far.
$ awk '/PID/{print;getline;print;exit} /mibd_interface/' snmpoutput.txt
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8670334 8484152 -186182 267657
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8369050 8514825 145775 126259
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8446906 8264885 -182021 322280
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8264884 8264960 76 284409
$ awk '/show clock/{getline;print}' snmpoutput.txt
Thu Sep 19 14:38:02.812 WIB <<<< capture this line
Thu Sep 19 14:42:42.425 WIB
Thu Sep 19 14:46:15.895 WIB
Thu Sep 19 14:50:44.213 WIB <<<< capture this line
$ awk '/mibd_interface/{x+=$5} END {print x}' snmpoutput.txt
-222352
我想要一些有关如何将这些部分结合在一起的指导.不一定要寻找解决方案,而是在逻辑上提供帮助.
I would like some guidance on how to join these parts together. Not necessarily looking for a solution, but assistance around the logic.
谢谢.
推荐答案
awk
是一种很棒的语言:
awk '
/PID/{
# store the line with PID NAME BLA BLA
pidline=$0;
# alse remember the line with ----------------------- minuses
getline;
minusline=$0
}
/mibd_interface/{
# remember the line with mibd_interface
mibdlines[mibdlen++]=$0;
# calculate some-things
diff += $5;
timediff += $6
}
# finally output!
END{
# output the PID NAME ...
print pidline;
# output ------------
print minusline;
# output the mibd_interface lines
for (i in mibdlines) print mibdlines[i];
# another ------------ line
print minusline;
# and output the difference, printf is there for a simple formatting
printf "%56s%-12d%-d\n"," ",diff,timediff
}'
或一个班轮:
awk '/PID/{pidline=$0;getline;minusline=$0} /mibd_interface/{mibdlines[mibdlen++]=$0; diff += $5; timediff += $6} END{ print pidline; print minusline; for (i in mibdlines) print mibdlines[i]; print minusline; printf "%56s%-12d%-d\n"," ",diff,timediff}'
输入输入输出时:
PID NAME MEM BEFORE MEM AFTER DIFFERENCE MALLOCS-NEW
-------------------------------------------------------------------------------
7582 mibd_interface 8670334 8484152 -186182 267657
7582 mibd_interface 8369050 8514825 145775 126259
7582 mibd_interface 8446906 8264885 -182021 322280
7582 mibd_interface 8264884 8264960 76 284409
-------------------------------------------------------------------------------
-222352 1000605
在第二次尝试中,无需使用mbd_interface存储这些行,因此我们可以在它们出现时将它们输出:
On the second try there is no need to store the lines with mbd_interface, so we can just output them as they come:
awk '
/PID/{
if(once == 0) {
#output the PID NAME line
print;
# output and remember the -------------- line
getline;
minusline=$0;
print;
}
once=1
}
/mibd_interface/{
# output the mibd line
print;
# calculate some-things
diff += $5; timediff += $6
}
END{
# print another minus line
print minusline;
# print the calculated some-things
printf "%56s%-12d%-d\n"," ",diff,timediff
}'
输出相同.或单线:
awk '/PID/{if(once == 0) { print; getline; minusline=$0; print; } once=1 } /mibd_interface/{print; diff += $5; timediff += $6} END{ print minusline; printf "%56s%-12d%-d\n"," ",diff,timediff}'
这篇关于协助awk/bash捕获内存差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文