In a very long line I'll summarize with:


I want to print only:


To match the second parenthesis, I exclude the word that follows the third one:

$ grep -oP "\\(.*(?!word).*right"

but Bash interprets the exclamation mark:

-bash: !word: event not found

Protecting the exclamation mark with single quote fails with grep: missing )

$ grep -oP '\\(.*(?!word).*right'
$ grep -oP '\\((?!word)(.*right)'

Protecting the exclamation mark with backslash fails with grep: unrecognized character after (? or (?-

Any idea?

Note: -P is for Perl regex and -o is to print only the matching part of a line

  • 1
    grep -o '(b[^)]*' works in that case. Your attempted usage of the negative look ahead operator doesn't make sense. What exactly do you want to match? – Stéphane Chazelas Mar 31 '14 at 14:03
  • 2
    use set +H to turn off history expansion, set -H to turn it back on. – glenn jackman Mar 31 '14 at 14:15
  • @glennjackman Or just use proper quoting: single quotes in this case. (Which gets to the next step that the regex isn't syntactically correct.) – Gilles 'SO- stop being evil' Mar 31 '14 at 20:06

The rules are different for single quotes versus double quotes.

For the reason you show, double quotes can't be used reliably in bash, because there's no sane way to escape an exclamation mark.

$ grep -oP "\\(.*(?!word).*right"
bash: !word: event not found

$ grep -oP "\\(.*(?\!word).*right"
grep: unrecognized character after (? or (?-

The second is because bash passes through \! rather than ! to grep. Showing this:

$ printf '%s' "\!"

When you tried single quotes, the double backslash doesn't mean an escaped backslash, it means two backslashes.

$ printf '%s' '\\(.*(?!word).*right'

Inside single quotes, everything is literal, and there are no escapes, so the way to write the regular expression you're trying is:

$ grep -oP '\(.*(?!word).*right'

If you want to match from the second open parentheses up until (but not including) the next closing parentheses:

grep -Po '\(.*?\K\([^)]*'

Or portably with sed:

sed -n 's/^[^(]*([^(]*\(([^)]*\).*/\1/p'

To match the right most ( that is not followed by word up to the rightmost right after that:

grep -Po '.*\K\((?!word).*right'

You can do it with simple awk:

$ echo '(foo),(bar,baz(word,right),(end)' | awk -F'),' '{print $2}'

Look for the comma parens before and aft instead:

grep -oP '(?<=,)\(.*(?=\),)'


$ echo '(foo),(bar,baz(word,right),(end)' | grep -oP '(?<=,)\(.*(?=\),)'

The lookahead and behind can only look for explicit strings, it cannot find things such as .*.



If for some reason you cannot use single quotes as suggested in Mikel's answer, you can temporarily turn off history expansion using set +H (turn it back on with set -H), as suggested by glenn jackman in comments.

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.