3

You can escape parenthesis in grep by standard adding \. But i want to escape parenthesis in egrep which uses \( as a grouping mechanism like sed. I want to write egrep expression to parse the following:

log_message(_sanitize("my string"));

JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51

2 Answers2

3

Parentheses in grep (BRE) require no escaping, they match themselves literally:

$ grep -o '(a)' <<< '(a)'
(a)

In egrep (ERE) parentheses are used for grouping. They can be escaped to match literal parentheses:

$ egrep -o '(a)' <<< '(a)'
a

$ egrep -o '\(a\)' <<< '(a)'
(a)

You can therefore just add escaping to yours to match the expression literally:

$ egrep -o 'log_message\(_sanitize\("my string"\)\);' \
    <<< 'log_message(_sanitize("my string"));'
log_message(_sanitize("my string"));
that other guy
  • 116,971
  • 11
  • 170
  • 194
2

One way is to put the ( in a character class like:

 egrep 'log_message[(]_sanitize[(]"my string"[)][)];' <file>
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67