在linux中减去后续行

时间:2016-02-24 15:38:08

标签: linux bash awk

我有一个这样的文件:

15:03:06:373 s1 13 s2 17 s3 15 s4 11 s5 7  15:03:06:450
15:03:06:555 s1 13 s2 19 s3 18 s4 15 s5 9  15:03:06:656
15:03:06:762 s1 13 s2 19 s3 21 s4 19 s5 13 15:03:06:833
15:03:06:939 s1 13 s2 19 s3 21 s4 19 s5 13 15:03:07:013

如何使用awk或某些脚本来减去行,以获得在行之间减去s1,s2等前面的值的结果(如row2-row1,row3-row2)。结果将是:

15:03:06:373 s1 13 s2 17 s3 15 s4 11 s5 7 15:03:06:450
15:03:06:555 s1 0  s2 2  s3 3  s4 4  s5 2 15:03:06:656
15:03:06:762 s1 0  s2 0  s3 3  s4 4  s5 4 15:03:06:833
15:03:06:939 s1 0  s2 0  s3 0  s4 0  s5 0 15:03:07:013

1 个答案:

答案 0 :(得分:2)

awk救援!

$ awk '{for(i=3;i<NF;i+=2) {t=$i;$i-=s[i];s[i]=t}}1' file

15:03:06:373 s1 13 s2 17 s3 15 s4 11 s5 7 15:03:06:450
15:03:06:555 s1 0 s2 2 s3 3 s4 4 s5 2 15:03:06:656
15:03:06:762 s1 0 s2 0 s3 3 s4 4 s5 4 15:03:06:833
15:03:06:939 s1 0 s2 0 s3 0 s4 0 s5 0 15:03:07:013

解释和对齐留作练习。