简体   繁体   English

如何计算两个相邻字段之间的值差异

[英]how to calculate value differences between two neighboring fields

I am trying to calculate distances between two neighboring fields. 我正在尝试计算两个相邻字段之间的距离。 My input file is like below. 我的输入文件如下所示。

1 11160 11533 11556 11731 11822 11870 12149 12411 12461 12686 12829 13315 13420 .... 1 11160 11533 11556 11731 11822 11870 12149 12411 12461 12686 12829 13315 13420 ....

In the output, I want to keep the first field, and the following field would be the value differences between the current field and the next field, $2=$3-$2 , $3=$4-$3 ... 在输出中,我想保留第一个字段,以下字段将是当前字段和下一个字段之间的值差异, $2=$3-$2$3=$4-$3 ...

A complete output will be like: 完整的输出将是:

1 373 23 175 91 48 279 262 50 225 143 486 105... 1 373 23 175 91 48 279 262 50 225 143 486 105 ......

How can I do this? 我怎样才能做到这一点?

In my code, each value is printed as a new line also the numbers are reversely printed. 在我的代码中,每个值都打印为一个新行,同时反向打印数字。

BEGIN {FS=" "}
{
        out[1]=$1
        for (i=2;i<=NF-1;i++) 
                out[i]=$(i+1)-$i
}
END{
        for (i in out)
               print out[i]
}

Here is current output 这是当前的输出

373 23 175 91 48 279 262 50 225 143 486 105 1 373 23 175 91 48 279 262 50 225 143 486 105 1

EDIT: Adding code suggested by anubhava sir too in comment section. 编辑:在评论部分添加anubhava先生建议的代码。

awk '{s=$1; for (i=2; i<NF; i++) s = s OFS $(i+1) - $i; print s}' Input_file

Could you please try following. 你可以尝试一下吗?

awk '{printf $1 OFS;for(i=2;i<NF;i++){printf("%d%s",$(i+1)-$i,i==(NF-1)?ORS:OFS)}}' Input_file

Output will be as follows. 输出如下。

1 373 23 175 91 48 279 262 50 225 143 486 105

Explanation: Adding explanation too here. 说明:此处也添加说明。

awk '
{
  printf $1 OFS                                 ##Printing first field and OFS(whose value is space by default).
  for(i=2;i<NF;i++){                            ##Starting for loop from value of 2 to till NF-1 value where NF is number of field in current line.
    printf("%d%s",$(i+1)-$i,i==(NF-1)?ORS:OFS)  ##Printing diffrence of next field and current field and checking condition for 2nd print if i==NF-1 then new line else print space for that line.
  }                                             ##Closing for loop block here.
}
' Input_file                                    ##Mentioning Input_file name here.
awk '{printf("%d",$1); for (i=2;i<NF;i++) printf(" %d",$(i+1)-$i)}' file

Output: 输出:

1 373 23 175 91 48 279 262 50 225 143 486 105

another awk 另一个awk

$ awk -v RS=' ' 'NR!=2{printf "%s ", $0-p} {p=$0}' file

1 373 23 175 91 48 279 262 50 225 143 486 105

you may want to add a final \\n . 你可能想要添加一个\\n

Your specification corresponds to displaying all pairwise diffs expect second one that's why there is NR!=2 code. 您的规范对应于显示所有成对差异,期望第二个,这就是为什么有NR!=2代码。 First field is compared to 0, so stays the same. 第一个字段与0进行比较,因此保持不变。

You almost have it! 你几乎拥有它! Just need to change the output from print out[i] to: 只需要将print out[i]更改为:

printf out[i] " "

The printf will not append a newline after each iteration, and instead you tack on a space. printf在每次迭代后都不会附加换行符,而是在空格上添加。

The output I recieved from the above adjustment turned into: 我从上述调整中收到的输出变为:

 1 373 23 175 91 48 279 262 50 225 143 486 105 

As far as the "numbers not making sense", they are as you are expecting, a difference between the consecutive numbers. 至于“没有意义的数字”,它们就像你期望的那样,是连续数字之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM