如何在 Bash/Grep 中转义单引号?

发布于 2024-12-02 03:49:55 字数 200 浏览 1 评论 0原文

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

something ~* 'bla'

我尝试了这个,但 shell 删除了单引号。啊...

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

正确的搜索是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭泣的笑容 2024-12-09 03:49:55

如果您确实需要在引号中查找引号,有一些丑陋的结构可以做到这一点。

echo 'And I said, "he said WHAT?"'

按预期工作,但对于另一级别的嵌套,以下无法按预期工作

echo 'She said, "And I said, \'he said WHAT?\'"'

相反,您需要将内部单引号转义到单引号字符串外部

echo 'She said, "And I said, '\''he said WHAT?'\''"'

或者,如果您愿意的话:

echo 'She said, "And I said, '"'"'he said WHAT?'"'"'"'

它不漂亮,但它有效。 :)

当然,如果你把东西放在变量中,所有这些都是没有意义的。

[ghoti@pc ~]$ i_said="he said WHAT?"
[ghoti@pc ~]$ she_said="And I said, '$i_said'"
[ghoti@pc ~]$ printf 'She said: "%s"\n' "$she_said"
She said: "And I said, 'he said WHAT?'"
[ghoti@pc ~]$ 

:-)

If you do need to look for quotes in quotes in quotes, there are ugly constructs that will do it.

echo 'And I said, "he said WHAT?"'

works as expected, but for another level of nesting, the following doesn't work as expected:

echo 'She said, "And I said, \'he said WHAT?\'"'

Instead, you need to escape the inner single quotes outside the single-quoted string:

echo 'She said, "And I said, '\''he said WHAT?'\''"'

Or, if you prefer:

echo 'She said, "And I said, '"'"'he said WHAT?'"'"'"'

It ain't pretty, but it works. :)

Of course, all this is moot if you put things in variables.

[ghoti@pc ~]$ i_said="he said WHAT?"
[ghoti@pc ~]$ she_said="And I said, '$i_said'"
[ghoti@pc ~]$ printf 'She said: "%s"\n' "$she_said"
She said: "And I said, 'he said WHAT?'"
[ghoti@pc ~]$ 

:-)

梦罢 2024-12-09 03:49:55
grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog

对我有用。

  • 转义第一个 * 以匹配文字 *,而不是使其成为零个或多个匹配字符:
    ~* 将匹配零次或多次出现的 ~,而
    ~\*something 之后的表达式 ~* 相匹配,
  • :alnum: 周围使用双括号(参见示例 此处
  • 使用 *之后[[:alnum::]] 不仅匹配单引号之间的一个字符,而且匹配其中的几个
  • 单引号,根本不需要转义,因为它们包含在由双引号限制的表达式中。
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.
南巷近海 2024-12-09 03:49:55
  • 字符类别用[[:alnum:]](两个括号)指定

  • [[:alnum:]] 仅匹配 一个 性格。匹配零个或多个字符[[:alnum:]]*

  • 您只需使用 " " 来引用正则表达式:

     grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog
    
  • character classes are specified with [[:alnum:]] (two brackets)

  • [[:alnum:]] is matching only one character. To match zero or more characters [[:alnum:]]*

  • you can just use " " to quote the regex:

      grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog
    
只是我以为 2024-12-09 03:49:55

根据您的表达式,您似乎首先使用 ',然后使用 "。如果您想转义单引号,可以使用 '< /code> 并转义它们,或者使用双引号,如 Matteo 注释,字符类有双正方形括号中的一个:

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

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

It seems, as per your expression, that you are using first ', then ". If you want to escape the single quotes, you can either use ' and escape them, or use double quotes. Also, as Matteo comments, character classes have double square brackets. Either:

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

or

grep -i 'something ~* \'[[:alnum:]]+\'' /var/log/syslog
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文