3

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.

  • well comma is not part of anything. man ascii is an ascii table, I don't really understand the question. – X Tian Oct 16 '15 at 12:13
  • , is commonly used instead of . in many european countries as the "decimal point" delimiter – cas Oct 16 '15 at 12:14
  • I went to school in Denmark – X Tian Oct 16 '15 at 12:15
  • I'm sorry but this is still an unclear question, it's not an ascii table, and there's no way to ascertain which radix the numbers are in. Are numbers in in a column in the same radix ? We can't see the numbers further down the list. – X Tian Oct 16 '15 at 12:21
  • 1
    @XTian I think the OP just means that the file format is plain (ascii) text rather than binary (not that it is supposed to contain a table of ascii codes) – steeldriver Oct 16 '15 at 12:24
3

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.

2

This will do the conversion:

awk '{print $1, $2, $3 * 10**$4}' file.csv

Assuming:

  1. There is an space bettween the e and the actual exponent.
  2. The exponent is the last field of each line.

If an specific format is needed, some form of printf could be used.

For example:

awk '{printf( "%s %s %g "ORS,$1,$2,$3*10**$4)}' file.csv

could print this:

1 1 1423 
1 2 1589 
1 3 85000 
1 4 8900 
1 5 8796 
2 8 2.589e+28

Last line added to show final format for big exponents.

2
{ sed 's/ /+/3' | xargs printf '%G% G% G\n'; } <in >out

1 1 1423
1 2 1589
1 3 85000
1 4 8900
1 5 8796

You'll wanna play around with the %G precision flags if the numbers get much larger than that.

1

Other variant with awk sprintf

awk 'NF==4{$(NF-1)=sprintf(CONVFMT, $(NF-1)$NF);NF=3}1' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.