shishi1181
shishi1181

Reputation: 67

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 ....

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 ...

A complete output will be like:

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

Upvotes: 5

Views: 75

Answers (4)

karakfa
karakfa

Reputation: 67507

another 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.

Your specification corresponds to displaying all pairwise diffs expect second one that's why there is NR!=2 code. First field is compared to 0, so stays the same.

Upvotes: 1

Cyrus
Cyrus

Reputation: 88644

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

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133528

EDIT: Adding code suggested by anubhava sir too in comment section.

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.

Upvotes: 2

Sunny Patel
Sunny Patel

Reputation: 8077

You almost have it! Just need to change the output from print out[i] to:

printf out[i] " "

The printf will not append a newline after each iteration, and instead you tack on a space.

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.

Upvotes: 0

Related Questions