views:

240

answers:

2

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

+2  A: 

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] }'
apocryphal
This solves it. Many thanks.
Arrieta
lose the cat and pass the file name to awk directly
ghostdog74
+1  A: 

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.

jetxee
In Fortran the D notation denotes a real number in double precision. So it is the same as `E` for all practical purposes. By the way, Python cannot read it either!
Arrieta