Reputation: 3344
I need help with this in AWK. I would like to subtract value of the second column from the first row from the first row from second column (20 - 15, 40 - 35, 45 - 45, etc.)
INPUT FILE
10 15
20 35
40 45
45 55
58 70
OUTPUT FILE
5
5
0
3
Thank you.
Upvotes: 1
Views: 5271
Reputation: 733
cat numb
10 15
20 35
40 45
45 55
58 70
so to perform action
awk 'p{print $1-p}{p=$2}' numb
output
5
5
0
3
Upvotes: 3
Reputation: 289525
You can do this:
$ awk 'NR==1 {a=$2; next} {print $1 - a; a=$2}' file
5
5
0
3
It keeps track of the 2nd value and stores in the variable a
.
NR==1 {a=$2; next}
if we are in the first record (line), just store the 2nd col.{print $1 - a; a=$2}
from that moment, print the difference between 1st column and stored value. Then, store the 2nd col of current line, to be used in the following line.Upvotes: 1