问题
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.
回答1:
Remember the old values (from the previous row).
awk 'NR > 1 { print $1 - old1, $2 - old2 }
{ old1 = $1; old2 = $2 }' data.file
回答2:
You can try this,
awk 'NR==1{col1=$1; col2=$2;} { print $1-col1,$2-col2; col1=$1;col2=$2}' yourfile
来源:https://stackoverflow.com/questions/23733863/awk-command-calculation-of-columns