How to subtract the adjacent lines from a single column?

Hi All,

I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column.

For Example:
(Input)

Col1

5
10
12
6
9
12
5
.
.
.
.

output should be like this:

Col1

5
2
-6
3
3
-7
.
.
.
.

Can anyone help me in this regard?

Expecting your reply and Thanks in advance.

Warm regards
Fredrick.

Try this:

awk 'p{print $0-p}{p=$0}' file

As always, we also appreciate any reaction from the Original Poster after an answer, thank you.

Frank, Can you please brief how this awk code works?

One in perl..

perl -e '@A=<>; while (++$i <=$#A ) { print $A[$i]-$A[$i-1],"\n"; }' file

For frankin's snippet,

awk 'p{print $0-p}{p=$0}' file

For the first time, as p will be valuated as FALSE and will not execute {print $0-p}. However, p will turn to a non-zero/TRUE value when it execute {p=$0}.From second record onwards, p will be TRUE and will evaluate {print $0-p} and print it.Also, it will set the new value(current line) to p

I got it. Thanks Dennis.

Franklin52's solution is probably the best, simplest, and shortest there is. However, that shouldn't stop unix userland lovers from having some fun :wink:

sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4

Only works with integers, as opposed to the AWK which will handle floating point as well, and assumes that the data file consists of nothing but one integer per line.

$ cat data
5
10
12
6
9
12
5

$ sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4
5
2
-6
3
3
-7

Cheers,
Alister

If we have zero in input file then we will not get the difference.

awk 'NR-1{print $0-p}{p=$0}' file
awk 'NR==1{key=$0} NR>1{print $0-key;key=$0;}'

Another way...

awk '{a[FNR]=$0;b[FNR]=$0}END{for(i=1;i<=FNR-1;i++) print b[i+1]-a}'

Hello, protocomm:

In this case, since they are not being modified, there is no point in having two identical copies of an array. You can simplify your approach to the following:

awk '{a[FNR]=$0}END{for(i=1;i<FNR;i++) print a[i+1]-a}'

Regards,
Alister

it's true ... thanx

Hello All,

Thank you very much for your replies. Its working fine.

In addition to the above query, I would like to extend the same from 'n' columns. That is, I have a file with 100 lines with 1000 columns, I would like to subtract each line with each other.

For example, (with 5 lines and 5 columns) 

Input
          Col1          Col2          Col3             Col4          Col5
line1      A1            B1            C1                D1            E1
line1      A2            B2            C2                D2            E2
line1      A3            B3            C3                D3            E3
line1      A4            B4            C4                D4            E4
line1      A5            B5            C5                D5            E5

I would like to subtract line1 with line1, line2, line3, line4 and line5, line2 with line1, line2, line3, line4 and line5, line3 with....so on ....
(all against each other)

Can any one help me in the regard?

Expecting your reply and thanks in advance.

Warm regards
Fredrick.

Fredrick, isn't your most recent question also being asked here? Is it homework? It really sounds like a homework question...