i have such a file
1.5000000000E-01 7.5714285714E+00 4.0000000000E-01
2.5000000000E-01 7.5714285714E+00 4.0000000000E-01
and i have to convert it to something like
0.15 7.57 0.40
i mean i want the numbers to be with only 2 decimals and not to be exponential. I want to use bash!
Using printf
with awk
:
$ awk '{printf "%.2f %.2f %.2f\n",$1,$2,$3}' file
0.15 7.57 0.40
0.25 7.57 0.40
Given more fields you would want to loop like:
$ awk '{for(i=1;i<=NF;i++)printf "%.2f ",$i;print "\n"}' file
0.15 7.57 0.40
0.25 7.57 0.40
This can work:
printf "%1.2f" $number
Test:
$ for i in 1.5000000000E-01 7.5714285714E+00 4.0000000000E-01 2.5000000000E-01 7.5714285714E+00 4.0000000000E-01;
do
printf "%1.2f\n" $i
done
0.15
7.57
0.40
0.25
7.57
0.40
In your case,
cat file | xargs printf "%1.2f\n"
Test:
$ cat file | xargs printf "%1.2f\n"
0.15
7.57
0.40
0.25
7.57
0.40
$ cat numbers.txt
1.5000000000E-01 7.5714285714E+00 4.0000000000E-01
2.5000000000E-01 7.5714285714E+00 4.0000000000E-01
$ while read F1 F2 F3;do printf "%.2f %.2f %.2f\n" $F1 $F2 $F3;done < numbers.txt
0.15 7.57 0.40
0.25 7.57 0.40
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With