Question

Hi guys I have the following lines:

A=3.5e30
B=4.345e40

(File contains columns with numbers in this notation 1.2345678D+10)

sed 's/D/E/g' File | awk '{print $1, $6*'$A', 10^($9)*'$B', $13, $8}' > File2

The output that I'm getting is:

3.168808781403E+02 29825999184755995994350665720659968 71343531834366140263241767594070376448 ... etc

How can I have the 2nd and 3rd column with the same notation as in the first column? I.e,

Instead of having : 71343531834366140263241767594070376448

I want: 7.134353183436E+37

Just to let you know the answer is:

 sed 's/D/E/g' File | awk '{printf "%.12E %.12E %.12E %.12E %.12E\n", $1, $6*'$A', 10^($9)*'$B', $13,$8 }' > File2
Was it helpful?

Solution

If you want the so called scientific notation use printf and %E format specifier.

Example:

echo 71343531834366140263241767594070376448 | awk '{printf "%E",$1}' 

Output:

7.134353E+37

OTHER TIPS

awk 'BEGIN { printf("%E\n", 71343531834366140263241767594070376448); }'
7.134353E+37

Simply use printf() instead of print():

$ awk 'BEGIN { printf "%e\n", 282048204209482034890 }' /dev/null
2.820482e+20

there are a couple of problems you need to fix in your codes:

  • usage of shell var in awk:

    awk -v awkVar1="$A" -v awkVar2="$B" ... '{use awkVar1 without $}' file
    
  • to get scientific-notation format, you need define the format with printf, one of %E, %e, %g, %G would be useful for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top