Linux 如何在 Bash/Grep 中转义单引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7254509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape single quotes in Bash/Grep?
提问by JohnnyFromBF
I want to search with grep for a string that looks like this:
我想用 grep 搜索如下所示的字符串:
something ~* 'bla'
I tried this, but the shell removes the single quotes argh..
我试过这个,但是shell删除了单引号argh ..
grep -i '"something ~* '[:alnum:]'"' /var/log/syslog
What would be the correct search?
什么是正确的搜索?
采纳答案by eckes
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~*
aftersomething
- use double brackets around
:alnum:
(see example here) - use a
*
after[[:alnum::]]
to match not only onecharacter 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.
- 转义第一个
*
匹配文字*
而不是使其成为零个或多个匹配字符:~*
将匹配零次或多次出现的~
while~\*
匹配~*
后面的表达式something
- 在周围使用双括号
:alnum:
(请参阅此处的示例) - 使用
*
after[[:alnum::]]
不仅可以匹配单引号之间的一个字符,还可以匹配其中的几个字符 - 单引号根本不必转义,因为它们包含在受双引号限制的表达式中。
回答by Matteo
- character classes are specified with
[[:alnum:]]
(two brackets) [[:alnum:]]
is matching only onecharacter. To match zero or more characters[[:alnum:]]*
you can just use
" "
to quote the regex:grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog
- 字符类用
[[:alnum:]]
(两个括号)指定 [[:alnum:]]
只匹配一个字符。匹配零个或多个字符[[:alnum:]]*
您可以使用
" "
引用正则表达式:grep -i "something ~\* '[[:alnum:]]*'" /var/log/syslog
Matteo
马泰奥
回答by Diego Sevilla
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:
根据您的表达,您似乎先使用'
,然后使用"
。如果要转义单引号,可以使用'
并转义它们,也可以使用双引号。此外,正如 Matteo 所评论的,字符类有两个方括号或者:
grep -i "something \~\* '[[:alnum:]]+'" /var/log/syslog
or
或者
grep -i 'something ~* \'[[:alnum:]]+\'' /var/log/syslog
回答by ghoti
If you doneed 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 workas expected:
按预期工作,但对于另一个级别的嵌套,以下内容无法按预期工作:
echo 'She said, "And I said, \'he said WHAT?\'"'
Instead, you need to escape the inner single quotes outsidethe 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 ~]$
:-)
:-)