简体   繁体   English

连续行之间的Linux差异

[英]Linux differences between consecutive lines

I need to loop trough n lines of a file and for any i between 1 and n - 1 to get the difference line(n - 1) - line(n) . 我需要循环通过文件的n行和i between 1 and n - 1任何i between 1 and n - 1来获得差异line(n - 1) - line(n)

And here is the source file: 这是源文件:

root@syncro:/var/www# cat cron.log | grep "/dev/vda"
/dev/vda          20418M 14799M     4595M  77% /
/dev/vda          20418M 14822M     4572M  77% /
/dev/vda          20418M 14846M     4548M  77% /
/dev/vda          20418M 14867M     4527M  77% /
/dev/vda          20418M 14888M     4506M  77% /
/dev/vda          20418M 14910M     4484M  77% /
/dev/vda          20418M 14935M     4459M  78% /
/dev/vda          20418M 14953M     4441M  78% /
/dev/vda          20418M 14974M     4420M  78% /
/dev/vda          20418M 15017M     4377M  78% /
/dev/vda          20418M 15038M     4356M  78% /
root@syncro:/var/www# cat cron.log | grep "/dev/vda" | cut -b 36-42 | tr -d " M"
4595
4572
4548
4527
4506
4484
4459
4441
4420
4377
4356

those /dev/vda... lines are logged hourly with df -BM in cron.log file and the difference between lines will reveal the hourly disk consumption. 这些/dev/vda...行每小时用cron.log文件中的df -BM记录,行之间的差异将显示每小时磁盘消耗。

So, the expected output will be: 所以,预期的产出将是:

23 (4595 - 4572)
24 (4572 - 4548)
...
43 (4420 - 4377)
21 (4377 - 4356)

I don't need the text between ( and ) , I put it here for explanation only. 我不需要()之间的文本,我把它放在这里只是为了解释。

I'm not sure if I got you correctly, but the following awk script should work: 我不确定我是否正确,但以下awk脚本应该工作:

awk '{if(NR>1){print _n-$4};_n=$4}' your.file

Output: 输出:

23
24
21
21
22
25
18
21
43
21

You don't need the other programs in the pipe. 不需要管道中的其他程序。 Just: 只是:

awk '/\/dev\/vda/ {if(c++>0){print _n-$4};_n=$4}' src/checkout-plugin/a.txt 

will be enough. 就足够了。 The regex on start of the awk scripts tells awk to apply the following block only to lines which match the pattern. 启动awk脚本的正则表达式告诉awk仅将以下块应用于与模式匹配的行。 A side effect is that NR can't be used anymore to detect the "second line" in which the calculation starts. 副作用是NR不能再用于检测计算开始的“第二行”。 I introduced a custome counter c for that purpose. 我为此目的介绍了一个客户柜台c

Also note that awk will remove the M on it's own, because the column has been used in a numeric calculation. 另请注意, awk会自行删除M ,因为该列已用于数值计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM