Linux differences between serial lines

I need to scroll through the lines of the n file and for any i between 1 and n - 1 get the difference 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 

these lines /dev/vda... are registered hourly with df -BM in the cron.log file, and the difference between the lines will indicate the consumption of the clock disk.

So, the expected result will be:

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

I do not need the text between ( and ) , I put it here just for explanation.

+8
linux bash line difference
source share
1 answer

I'm not sure if I got you right, but the following awk script should work:

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

Output:

 23 24 21 21 22 25 18 21 43 21 

You do not need other programs in the pipe. Just:

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

will be sufficient. The regular expression when running awk scripts tells awk to apply the following block only to lines that match the pattern. A side effect is that NR can no longer be used to detect the โ€œsecond lineโ€ at which the calculation begins. For this purpose, I introduced the custome c counter.

Also note that awk will delete M its own because the column was used in numerical computation.

+11
source share

All Articles