bc
doesn't like numbers expressed in scientific notation (aka exponential notation).
$ echo "3.1e1*2" | bc -l
(standard_in) 1: parse error
but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc
to understand exponential notation? If not, what can I do to translate them into a format that bc
will understand?
Try this: (using bash)
or this:
If you have positive exponents you should use this:
That last one would handle every numbers thrown at it. You can adapt the 'sed' if you have numbers with 'e' or 'E' as exponents.
You get to chose the scale you want.
One can use awk for this; for example,
produces (via awk's default format %.6g) output like
12.3457 3.14159 543210000000000000
while commands like the following two produce the output shown after each, given that file
edata
contains data as shown later.Also, regarding solutions using
sed
, it probably is better to delete the plus sign in forms like45e+3
at the same time as thee
, via regex[eE]+*
, rather than in a separatesed
expression. For example, on my linux machine with GNU sed version 4.2.1 and bash version 4.2.24, commandssed 's/[eE]+*/*10^/g' <<< '7.11e-2 + 323e+34'
sed 's/[eE]+*/*10^/g' <<< '7.11e-2 + 323e+34' | bc -l
produce output
7.11*10^-2 + 323*10^34
3230000000000000000000000000000000000.07110000000000000000
Piping version of OPs accepted answer
Piping the input to the OPs accepted sed command gave extra backslashes like