-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
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

  • 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

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