difference between number in the same column using AWK

后端 未结 4 472
故里飘歌
故里飘歌 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:24

    In case awk is not a strict requirement, a shell solution:

    set -- $(< file)
    p=$1; shift; while (($# > 0)); do echo $(($1 - p)); p=$1; shift; done
    

    DRYer

    set -- $(< file)
    while (($#>0)); do [[ -n $p ]] && echo $(($1-p)); p=$1; shift; done
    

提交回复
热议问题