Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What regular expression should I use in order to remove exponential number terms and floating point numbers from a text file?

For example I have the following terms in my file:

-0.161678
relational
0.081827
5.936420e-01
-0.155506
Favori54
0.086938
1.187284e+00
-0.149283
0.091921
"External"
1.780926e+00
-0.143025

I would like my file only includes the following lines:

relational
Favori54
"External"
share|improve this question
    
How about this example? regexr.com?36grk It can probably refined/optimised, since this one might be slightly crude, but it does work for the example you provided. :) – omninonsense Sep 27 '13 at 18:41

It looks to me like you want lines that begin with anything but a digit or the minus sign. This will get you that:

grep '^[^0-9,-]' file.txt
share|improve this answer
    
Did you test this? It won't work. The regular expression will only match any first non-digit or non-minus sign. Also even if you add the + symbol to the end of that, it would still not match for Favori54 – omninonsense Sep 27 '13 at 19:20
    
I did test it, with the lines listed in the OP, and it worked for me. grep prints any lines containing the pattern -- in this case, any line that has any non-digit-or-minus character as its first character. Since the tags specify linux, grep will be available. – evilsoup Sep 27 '13 at 19:28
    
Thanks for your solution but it's not as general as I need. The following one actually get all the things that I don't need. egrep -x '\-''\+''[[:digit:]]+''\.''[[:digit:]]+''\e''[[:punct:]]''[[:digit:]]+' How can I just reverse this?\ – Umit Karabiyik Sep 27 '13 at 19:34
    
Found the solution! I just needed to ad -v parameter. Here is most general version: egrep -vx '\-''\+''[[:digit:]]+''\.''[[:digit:]]+''\e''[[:punct:]]''[[:digit:]]+' – Umit Karabiyik Sep 27 '13 at 19:54
    
You should write that up as an answer (you can accept your own answer after two days IIRC). – evilsoup Sep 27 '13 at 20:00
awk '/^[+-]?[0-9]+\.?[0-9]*([Ee][+-]?[0-9]+)?$/ { next }
     /^[+-]?\.[0-9]+([Ee][+-]?[0-9]+)?$/ { next }
     { print }' yourfile

The mantissa part of the number, can begin with a dot, or end with a dot, but cannot just be a dot: . isn't valid, and adding an exponent like .E3 doesn't make it valid. 0. and .0 are valid, with or without an exponent. The exponent may be an upper or lower case e, and may have an explicit + sign.

Note that this assumes no whitespace around the floating-point values. It also matches integer values. Furthermore, it passes through items which differ from the floating-point notation with junk characters or whatever, like 1E+, 1.0A or .0E+33x. Note that some of these examples have a floating-point notation as a valid prefix.

The two regular expressions can be combined to avoid repetition of the common exponent part, and the common rule action. The result is even less readable, though.

Lastly, { print } can be "code golfed" down to just 1. The expression 1 is Boolean true, and awk's default action for a true condition or matching pattern is { print }.

share|improve this answer

Try this:

awk '{if($1+0!=$1) print $0}' filename.txt

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .