Awk command - calculation of columns

2019-03-05 04:57发布

I am very new in Awk. I want to calculate the difference between first row column2 and second row column2. For instance:

Num1 Num2 
23   26
34   39
43   58
63   61

So, I want to calculate from Column (Num1) 34-23, 43-34, 63-43. And same for column(Num2). Can you please help me. I can only calculate the value within rows which is $1 - $2, but not within column.

2条回答
对你真心纯属浪费
2楼-- · 2019-03-05 05:32

You can try this,

awk 'NR==1{col1=$1; col2=$2;} { print $1-col1,$2-col2; col1=$1;col2=$2}' yourfile
查看更多
Bombasti
3楼-- · 2019-03-05 05:50

Remember the old values (from the previous row).

awk 'NR > 1 { print $1 - old1, $2 - old2 }
            { old1 = $1; old2 = $2 }' data.file
查看更多
登录 后发表回答