如何在 Bash/Grep 中转义单引号?
我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确实需要在引号中查找引号,有一些丑陋的结构可以做到这一点。
按预期工作,但对于另一级别的嵌套,以下无法按预期工作:
相反,您需要将内部单引号转义到单引号字符串外部 :
或者,如果您愿意的话:
它不漂亮,但它有效。 :)
当然,如果你把东西放在变量中,所有这些都是没有意义的。
:-)
If you do need to look for quotes in quotes in quotes, there are ugly constructs that will do it.
works as expected, but for another level of nesting, the following doesn't work as expected:
Instead, you need to escape the inner single quotes outside the single-quoted string:
Or, if you prefer:
It ain't pretty, but it works. :)
Of course, all this is moot if you put things in variables.
:-)
对我有用。
*
以匹配文字*
,而不是使其成为零个或多个匹配字符:~*
将匹配零次或多次出现的~
,而~\*
与something
之后的表达式~*
相匹配,:alnum:
周围使用双括号(参见示例 此处)*
之后[[:alnum::]]
不仅匹配单引号之间的一个字符,而且匹配其中的几个works for me.
*
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~*
aftersomething
:alnum:
(see example here)*
after[[:alnum::]]
to match not only one character between your single quotes but several of them字符类别用
[[:alnum:]]
(两个括号)指定[[:alnum:]]
仅匹配 一个 性格。匹配零个或多个字符[[:alnum:]]*
您只需使用
" "
来引用正则表达式: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:根据您的表达式,您似乎首先使用
'
,然后使用"
。如果您想转义单引号,可以使用'< /code> 并转义它们,或者使用双引号,如 Matteo 注释,字符类有双正方形括号中的一个:
或
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:or