Operating System - HP-UX
1753481 Members
3967 Online
108794 Solutions
New Discussion юеВ

print minus result by using awk

 
SOLVED
Go to solution
Matthew_50
Valued Contributor

print minus result by using awk

# cat input
22855645598480 43572718341060
22855645621802 43572718419947

Is it possible to use awk to do following ?

1st column: 2nd line minus 1st line then divided by 5000, then print result
2nd column: 2nd line minus 1st line then divided by 5000, then print result

or perl is also welcome
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: print minus result by using awk

Hi Matthew:

# awk '{if (NR==1) {A=$1;B=$2;next};X=$1-A;print X;Y=$2-B;print Y}' file

Regards!

...JRF...
Matthew_50
Valued Contributor

Re: print minus result by using awk

Thanks, this works.

awk '{if (NR==1) {A=$1;B=$2;next};X=($1-A)/5000;print X;Y=($2-B)/5000;print Y}' input
Matthew_50
Valued Contributor

Re: print minus result by using awk

awk '{if (NR==1) {A=$1;B=$2;next};X=($1-A)/5000;print X;Y=($2-B)/5000;print Y}' input
Dennis Handly
Acclaimed Contributor

Re: print minus result by using awk

If only the two lines:
awk <{
n1 = $1; n2 = $2 # save
getline
print ($1 - n1) / 5000, ($2 - n2) / 5000
exit
} ' input