Question

My problem is that in among a column of data there are a few values that are exponentials, usually in the form of 2.796e-05. The decimal value in front of the 'e' always varies but it seems like its always to e-05.

So what i've been trying so far (having tried various amounts of changes in the hopes it'll work) is:

sed -i -e 's/[0-9]*\.[0-9]*e-05/0/g' datafile.txt

All of my attempts so far have not had any result. Now im sure im just leaving out one little thing or something similar to that extent but I just cant see it, so any help will be appreciated.

As requested: The input is a large file in the form of

19990207 8.891 6.1756 07/02/1999 9.707767 Q31
19990208 0.87234 5.2431 08/02/1999 10.239032 Q31
19990209 4.8225e-06 4.2400 09/02/1999 12.312976 Q31
19990210 0.00013825 5.3127 10/02/1999 10.353386 Q31
(The above is tab seperated but I have no idea how to show this here)

For the output I basically just wont the bolded part to be 0. But instead of getting 0's in the places where there are exponentials nothing seems to happen and the output remains the same as the input.

The script in which the code is used is a bit long but I can post it if it will help.

Was it helpful?

Solution

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 so you should have it.

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