Awk command - calculation of columns

隐身守侯 提交于 2019-12-02 17:50:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!