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?
Best Answer
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 ofgrep
, 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:
Also note that I replaced
-E
with-P
, because negative lookahead(?!...)
doesn't work in typical implementations ofgrep
extended regex. The-P
flag enables Perl regular expressions, which should support this.