re='-?([[:digit:]]+(\.[[:digit:]]*)?|[[:digit:]]*\.[[:digit:]]+)[Ee]-[[:digit:]]+'
sed -r "s/$re/0/g" file
That regular expression will accept numbers like -3e-3
, 3.1e-1234
, .14159E-01
-- there is:
- an optional negative sign
- followed by either
- mandatory digits and optional dot and fractional digits or
- optional digits and a mandatory dot and fractional digits
- followed by E or e and a negative integer.
I use the POSIX [:digit:]
character class because it's available.
I save the regular expression in a variable to make maintenance a little easier. Then you have to use double quotes around the sed program body so the shell variable can be substituted.
Using sed's -r
flag means you can avoid a lot of backslash escapes. It is (I think) a GNU sed extension, but you tagged your question linux so you should have it.