TransWikia.com

Convert all exponential numbers to decimal numbers in a file in Linux?

Unix & Linux Asked by user10345633 on January 19, 2021

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.10fn", $1}'

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

Thank you in advance!

One Answer

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.

Correct answer by thanasisp on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP