Search code examples
linuxperlawknumericalscientific-notation

how to change scientific notation to numeric in a specific column in linux or perl


I have a file as follows:

1   13.00000    12.9999999183917    8.160832898340686E-008  21148294
2   16.00000    15.9999995223584    -4.760327190211910E-007 21148294
3   9.000000    8.99999979262170    2.073783047507050E-007  21148295

I want an output like:

1   13.00000    12.9999999183917    0.0000000816083289340686    21148294
2   16.00000    15.9999995223584    -0.00000004760327190211910  21148294
3   9.000000    8.99999979262170    0.00000002073783047507050   21148295

Can anyone help me ? Thanks a lot!!


Solution

  • the key to this problem is to use printf to control the precision.

    You should give an expected precision, e.g. the numbers in your 1st row and 2nd row have different precision. which one do you expect? or even a higher one? here is the example with awk. do some test on your own and pick the right one. It should at least show you the way, to get you start:

    kent$  cat f
    1   13.00000    12.9999999183917    8.160832898340686E-008  21148294
    2   16.00000    15.9999995223584    -4.760327190211910E-007 21148294
    3   9.000000    8.99999979262170    2.073783047507050E-007  21148295
    
    kent$  awk '{$4=sprintf("%.23f",$4)}7' f
    1 13.00000 12.9999999183917 0.00000008160832898340686 21148294
    2 16.00000 15.9999995223584 -0.00000047603271902119099 21148294
    3 9.000000 8.99999979262170 0.00000020737830475070501 21148295
    
    kent$  awk '{$4=sprintf("%.24f",$4)}7' f 
    1 13.00000 12.9999999183917 0.000000081608328983406864 21148294
    2 16.00000 15.9999995223584 -0.000000476032719021190989 21148294
    3 9.000000 8.99999979262170 0.000000207378304750705009 21148295
    

    you can change the %.xxf pattern