简体   繁体   中英

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}'

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 .

反斜杠应该可以解决问题

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM