如何在Bash / Grep中避免单引号? [英] How to escape single quotes in Bash/Grep?

查看:267
本文介绍了如何在Bash / Grep中避免单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用grep搜索一个如下所示的字符串:

  something〜*'bla'

我尝试过这个,但是shell删除了单引号argh ..

  grep -i'something〜*'[:alnum:]''/ var / log / syslog 
pre>

什么是正确的搜索?

解决方案

  grep -isomething〜\ *'[[:alnum:]] *'/ var / log / syslog 

适用于我。




  • 转义第一个 * 匹配文字 * ,而不是使其成为零或多匹配字符:

    〜* 将匹配零个或多个出现的

    〜\ * 之后的表达式〜* 匹配

  • 使用在之间的双括号:alnum:(参见示例 here

  • 使用 * after [[:alnum ::]] 不仅可以匹配单引号之间的一个字符,还有几个

  • 单引号不需要转义,因为它们包含在受双引号限制的表达式中。


I want to search with grep for a string that looks like this:

something ~* 'bla'

I tried this, but the shell removes the single quotes argh..

grep -i '"something ~* '[:alnum:]'"' /var/log/syslog

What would be the correct search?

解决方案

grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog

works for me.

  • escape the first * to match a literal * instead of making it the zero-or-more-matches character:
    ~* would match zero or more occurrences of ~ while
    ~\* matches the expression ~* after something
  • use double brackets around :alnum: (see example here)
  • use a * after [[:alnum::]] to match not only one character between your single quotes but several of them
  • the single quotes don't have to be escaped at all because they are contained in an expression that is limited by double quotes.

这篇关于如何在Bash / Grep中避免单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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