0

I am trying to find out if any of the floats in the first column containing floats in a CSV have a 1 after the decimal point. Here is an example row from my CSV:

1082805252197942751,34225793738713276,serialtooldata,1,Goals,75512140,125.0,63.0,29.0,1,,,,0,899,,

The seventh column is the one I am interested in, and in the row above it does not have a 1 after the decimal point (125.0)

I've come up with two potential regular expressions:

^[0-9]+,[0-9]+,[a-z0-9]+,[0-9]+,[A-Za-z0-9\ ]+,[0-9]+,[0-9]+\.1

or

^[A-Za-z0-9\ ,]+\.1

These both seem to work, i.e. on a regex test page (like regex101 here and here) the line correctly fails to match either regex. But when I try them at the terminal like this

egrep ^[0-9]+,[0-9]+,[a-z0-9]+,[0-9]+,[A-Za-z0-9\ ]+,[0-9]+,[0-9]+\.1 tool_data_160321.csv

or

egrep ^[A-Za-z0-9\ ,]+\.1 tool_data_160321.csv

then the line is listed as a match. Why does egrep think the line matches either regex when it doesn't?

1
  • FYI To make extra sure that what you're trying to match is actually what you want to match, just use the -o flag. Mar 24 at 18:11
5

Because you are missing quotes. The backslash is interpreted by the shell, and the grep command gets just .1, not \.1, so it matches any character followed by 1.

It's best to quote the whole string:

egrep '^[A-Za-z0-9\ ,]+\.1' tool_data_160321.csv

Or in this case quote the quote character:

egrep ^[A-Za-z0-9\ ,]+\\.1 tool_data_160321.csv

Note that the second example can still lead to surprises depending on files present, so just quote the whole string, that is never wrong.

3
  • Thanks! The other solution I just found was to include the dot as the only option, as it does not need escaping inside option brackets, i.e. ^[A-Za-z0-9\ ,]+[.]1
    – dumbledad
    Mar 24 at 14:47
  • 1
    Yes, you can rewrite the regex to not need a backslash. Note also that space doesn't require a backslash, whether in a character set or outside. But it is needed to prevent the shell from splitting the regex. Just use single quotes unless you are sure you don't need them.
    – RalfFriedl
    Mar 24 at 14:52
  • 1
    And in default Zsh, or in Bash with failglob set, the second will error out, so better just quote everything that looks like a shell glob. Also, the ERE [A-Za-z0-9\ ,] will match a backslash too. In the first command here egrep does see it because it's quoted.
    – ilkkachu
    Mar 24 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.