使用AWK在同一列中的数字之间的差异 [英] difference between number in the same column using AWK

查看:93
本文介绍了使用AWK在同一列中的数字之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一列数字的文件:

I have a file containing one column of number:

1
2
4
4
10

我想使用awk计算每个数字之间的差.输出应该是这样的:

I would like to calculate the difference between each number using awk. The output should be like this :

1
2
0
6

我该怎么办?

推荐答案

尝试以下代码:

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

解释

  • NR 是从输入开始的当前记录的序号.在BEGIN动作内,该值应为零.在END操作中,该值应为最后处理的记录的编号.
  • next 语句将导致放弃对当前输入记录的所有进一步处理.如果下一条语句出现或在BEGIN或END中调用,则行为未定义 行动.
  • 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.
    • 这篇关于使用AWK在同一列中的数字之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆