I am trying to run a grep regular expression on a file, where I have to exclude lines where "00" and "0" appear. I came up with this expression:

grep -a -E \"stored\"\:\ \"\*123\*(?!00)[0-9]{2,5}\#\" $filename

But when I try to run it in bash, I either get

-bash !00: event not found, or (once I have typed set +H),

-bash: syntax error near unexpected token('`

Please what do I need to do to properly escape this regular expression in bash?


You get an error message because you did not escape !, which has a special meaning in the shell. To use that as part of the pattern parameter of grep, you must quote it or escape it.

Instead of quoting every single special character one by one, it's a lot easier to enclose the entire pattern within single or double-quotes (depending on your purpose), for example:

grep -a -P '"stored": "*123*(?!00)[0-9]{2,5}#"'

Also note that I replaced -E with -P, because negative lookahead (?!...) doesn't work in typical implementations of grep extended regex. The -P flag enables Perl regular expressions, which should support this.


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.