2

I have checked all the previous posts related to this, but I cannot find the way that I wanted to do.

I have a file with some exponential numbers as below. I do not know which columns have exponential numbers.

file1.txt

1 499 5e-29 0.33 1.35 46.65
5 999 0.4444 3e-6 0.556 89.444
many more lines

I want to convert all the exponential numbers to decimal numbers.

If I want to convert only one number, I could do as below.

echo 12.34567E-3 | awk '{printf "%5.10f\n", $1}'

But in this way, does anybody know how to do?

Thank you in advance!

2
  • Do you want to apply the same formatting (for example 10 decimal points) for all existing numbers or only for the converted ones?
    – thanasisp
    Nov 24, 2020 at 6:34
  • Thank you for your comment, thanasisp. No need to apply the same formatting. Thanks! Nov 24, 2020 at 6:36

1 Answer 1

3

You can loop for all fields of each line. Testing for if the field is a numerical value ($i+0==$i) and (&&) if it contains the character e seems good. So we modify only these fields to decimals.

Here using GNU awk sprintf function:

awk '{
    for (i=1;i<=NF;i++) if ($i+0 == $i && $i ~ /e/) $i = sprintf("%.10f", $i)
} 1' file
  • You can use any format instead of .10f into there.
  • 1 at the end means the default action, to print the line.
1
  • Thank you so much!!! This perfectly worked!! Nov 24, 2020 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .