difference between number in the same column using AWK

后端 未结 4 505
故里飘歌
故里飘歌 2021-01-04 07:57

I have a file containing one column of number:

1
2
4
4
10

I would like to calculate the difference between each number using awk. The outpu

4条回答
  •  情书的邮戳
    2021-01-04 08:02

    Try the following code :

    awk '
        NR == 1{old = $1; next}     # if 1st line 
        {print $1 - old; old = $1}  # else...
    ' file.txt
    1
    2
    0
    6
    

    explanations

    • NR is the ordinal number of the current record from the start of input. Inside a BEGIN action the value shall be zero. Inside an END action the value shall be the number of the last record processed.
    • next statement shall cause all further processing of the current input record to be abandoned. The behavior is undefined if a next statement appears or is invoked in a BEGIN or END action.

提交回复
热议问题