在grep中转义感叹号? [英] Escaping the exclamation point in grep?

查看:248
本文介绍了在grep中转义感叹号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个完整的行(下面的awk命令是rpm命令),我想从某些文件(包括引号)中删除。当我尝试grep和grep -F时,我似乎无法获得正确的输出。我试图从grep语句中删除部分尾行,它看起来像!造成问题。我试图用单引号包装字符串,也没有运气。谢谢。

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


解决方案


  1. 使用单引号 - 当您需要单引号时,只需从单引号中删除,将其中一个加上反斜杠,然后返回:



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


  2. 使用双引号和反斜杠 - 不仅可以感叹号,还可以用shell替换其他所有内容:

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


  3. 一个POSIX字符串:



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




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



    或者,如果您检查 $ 2 ==c以及 $ 2!=c,你可以这样做:



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



  5. 我会使用POSIX字符串,也可能是简单的单引号选项 - 它具有在其他shell中工作的优势,如 dash


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

    解决方案

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

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

    2. Use double-quotes and backslash-escape not only the exclamation point, but everything else that would otherwise get replaced by the shell:

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

    3. Use a POSIX string:

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

    4. 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.

    这篇关于在grep中转义感叹号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆