Escaping special characters in grep regex

15,023

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.

Share:
15,023

Related videos on Youtube

SinaOwolabi
Author by

SinaOwolabi

Updated on September 18, 2022

Comments

  • SinaOwolabi
    SinaOwolabi over 1 year

    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?