AWK subtract numeric record two from record one
I cannot solve a very simple problem. My data file looks like:
Crap Crap 0.123456789D+09 Crap Crap
Crap Crap 0.123456798D+09 Crap Crap
I need to use AWK to subtract the number in the third column; the SECOND row minus the FIRST row.
I tried:
cat crap.txt | awk '{ A[NR-1] = $3 } END { print A[1] - A[0] }'
to no success. Maybe the format of the number is wrong? (Can AWK read scientific notation with D
instead of E
?)
Help!
EDIT:
Just so the community knows, AWK does not understand scientific notation which uses D
instead of E
(like many Fortran outputs produce). It is necessary to replace the D
for E
and then carry on any mathematical operat开发者_开发百科ion.
You could change the notation on the fly...
cat crap.txt | awk '{ sub(/D/,"E",$3); A[NR-1] = $3; } END { print A[1] - A[0] }'
awk
cannot read D-notation (what does it mean by the way?). Here is an example:
printf "1.2D+1\n1.2E+1\n12\n" | awk '{ print $1/3; }'
0.4
4
4
The first one is wrong.
精彩评论