tim tran
tim tran

Reputation: 755

Escaping the exclamation point in grep?

I have this full line (the rpm command into awk below) that I want to grep out from certain files, including the quotes. I can't seem to be able to get the right output when I try grep, and grep -F. I tried deleting part of the tail end of line from the grep statement and it seems like the "!" causing the problems. I tried wrapping the string in single quotes and there is no luck as well. Thank you.

rpm -qVa | awk '$2!="c" {print $0}'

Upvotes: 8

Views: 11032

Answers (2)

Mark Reed
Mark Reed

Reputation: 95325

You have a few options.

  1. Use single quotes - when you need a literal single quote, just drop out of single quotes, add one with a backslash, and then go back in:

    grep 'rpm -qVa | awk '\''$2!="c" {print $0}'\' filename

  2. Use a POSIX string:

    grep $'rpm -qVA | awk \'$2!="c" {print $0}\'' filename

  3. Use a less-specific pattern:

    grep 'rpm -qvA | awk .$2.="c" {print $0}.' filename

    or, if you have checks for $2=="c" as well as $2!="c", you could do something like this:

    grep 'rpm -qvA | awk .$2[^=]="c" {print $0}.' filename

I would go with the POSIX string, or maybe the plain single-quote option - which has the debatable advantage of working in other shells, like dash.

Upvotes: 11

CuriousMind
CuriousMind

Reputation: 34155

A backslash should do the trick

rpm -qVa | awk '\$2!="c" {print $0}'

Upvotes: -1

Related Questions