-1

Can anyone explain why this does not work?

grep -ne '[A-Za-z]{1,30}\ [A-Z][a-z]{1,30}\W[A-Za-z]{1,30}\ [0-9]{1,30}\W[0-9]{5}\ [A-Za-z]{1,30}(\ [A-Za-z]{1,30})?' emails
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Richard
  • 99

1 Answers1

3

First, you are using extended regular expression (ERE) syntax like {}, () and ?. Add option -E to use ERE or do escape {} and () with backslashes and replace ? like this: grep -ne '[A-Za-z]\{1,30\} [A-Z][a-z]\{1,30\}\W[A-Za-z]\{1,30\} [0-9]\{1,30\}\W[0-9]\{5\} [A-Za-z]\{1,30\}\( [A-Za-z]\{1,30\}\)\{0,1\}' emails

Second, you are escaping spaces with backslashes inside single quotes. While most implementations of grep will handle this as simple spaces, the posix standard considers this undefined: The interpretation of an ordinary character preceded by a backslash ( '\' ) is undefined.

There may be additional problems, but we can't know without knowing the syntax of your file and what you want to grep

Philippos
  • 13,453
  • 2
    [A-Z] is also undefined other than in the C/POSIX locale per posix. In practice, it generally matches more characters (even possibly collating sequences made of more than one character) than the 26 letters of the english alphabet. – Stéphane Chazelas Apr 28 '17 at 10:49
  • Given that the pattern is not anchored, the \( [A-Za-z]\{1,30\}\)\{0,1\} is also superfluous. Possibly -x is missing. – Stéphane Chazelas Apr 28 '17 at 10:51