Reputation: 73089
I want to match all floating point numbers in the input and replace them with floating point numbers in a different format.
Here is something I tried:
echo "123.45E+00 blah 678.90E+00 blah 3.1415926535897932E+0000" \
| awk '{gsub(/[0-9]+\.[0-9]+\E\+[0-9]+/,sprintf("%E","&")); print $0}'
Unfortunately, the "&"
in the sprintf is a string and not a number so the result is:
0.000000E+00 blah 0.000000E+00 blah 0.000000E+00
That's the numeric format I want but the use of "&"
is resulting in zero as the value of the parameter, rather than the matched number.
Changing the "%E"
format to "%s"
works in that the original string is returned.
The output I'm wanting for the above input is:
1.234500E+02 blah 6.789000E+02 blah 3.141593E+00
Upvotes: 1
Views: 613
Reputation: 200233
Try something like this:
echo "123.45E+00 blah 678.90E+00 blah 3.1415926535897932E+0000" |
awk '{
for (i=1; i<=NF; i++)
sub(/[0-9]+\.[0-9]+\E\+[0-9]+/, sprintf("%.6E", $i), $i)
}1'
Upvotes: 2