Bash – Convert floating point numbers to decimal numbers

bashfloating pointtable

I have several 'ascii' tables in a directory, some of them have numbers expressed in decimal and some others ones in floating point, as follow:

1 1 1423
1 2 1589
1 3 0.85e 5
1 4 0.89e 4
1 5 8796
...

Is there a way to convert all the value of the tables in decimal numbers?
I heard that using tr editor might be useful but I canĀ“t figure out how to operate the conversion.

Best Answer

The sed script gets rid of the space after the 'e', and the awk script just prints out each field (multiplying $3 by 1 to "convert" it to a non-fp decimal number):

$ sed -e 's/e /e/g' file | awk '{print $1, $2, $3 * 1}'
1 1 1423
1 2 1589
1 3 85000
1 4 8900
1 5 8796

This assumes that the floating point numbers in the file:

  1. have an extraneous space after the 'e'
  2. omit the '+' for positive exponents
  3. don't have really large exponents otherwise awk will print them as fp.

It's possible to get awk to do the 's/e /e/' transformation (so sed isn't needed) but it's getting late and my brain's tired. sed | awk is easy and it works.

Related Question