grep中的转义字符 [英] Escape characters in grep
问题描述
我对于在grep的正则表达式中需要多少个反斜杠来逃避交替运算符|
感到有些困惑.这个
I'm a little confused about how many backslashes are needed to escape the alternation operator |
in regular expressions for grep. This
echo abcdef | grep -e"def|zzz"
不输出任何内容,因为grep不在扩展的正则表达式模式下.使用一个反斜杠进行转义,
outputs nothing, because grep is not in extended regex mode. Escaping with one backslash works,
echo abcdef | grep -e"def\|zzz"
打印abcdef
.更令人惊讶的是,使用2个反斜杠转义也可以,
prints abcdef
. More surprisingly, escaping with 2 backslashes also works,
echo abcdef | grep -e"def\\|zzz"
打印abcdef
.用三个反斜杠转义失败,
prints abcdef
. Escaping with three backslashes fails,
echo abcdef | grep -e"def\\\|zzz"
不打印任何内容.
有人有解释吗,特别是在2反斜杠的情况下?
Does anyone have an explanation, especially for the 2-backslash case ?
使用此简单的参数打印程序
Using this simple argument-printing program,
void main(int argc, char** argv)
{
for (int i = 0; i < argc; i++)
printf("Arg %d: %s\n", i, argv[i]);
}
我研究了我的shell用上面的命令行做什么:
I investigated what my shell does with the command lines above :
-e"def|zzz"
变为-edef|zzz
-e"def\|zzz"
变为-edef\|zzz
-e"def\\|zzz"
变为-edef\\|zzz
-e"def\\\|zzz"
变为-edef\\\|zzz
因此,所有双引号都将被删除,并且反斜杠和竖线不会被外壳程序更改.我怀疑grep本身对文字字符串\\|
做了特殊的事情.
So all double-quotes are removed and the backslashes and pipes are not altered by the shell. I suspect grep itself does something special with the literal string \\|
.
推荐答案
小写的-e
选项用于表示多个搜索操作.暗示着这种交替:
The lowercase -e
option is used to express multiple search operations. The alternation is implied:
$ echo abcdef | grep -e 'def' -e'zzz'
abcdef
$ echo abczzz | grep -e 'def' -e'zzz'
abczzz
或者,您可以将大号-E
选项用于扩展的正则表达式表示法:
Alternatively, you can use the upper -E
option for extended regular expression notation:
$ echo abcdef | grep -E 'def|zzz'
abcdef
我相信这可以直接解决您的问题(使用-e
进行替换或使用-E
进行扩展的正则表达式表示法).希望这会有所帮助:-)
I believe this solves you problem directly (either using -e
for alternation or -E
for extended regex notation). Hope this helps :-)
FWIW,反斜杠的问题是|
对于bash具有特殊含义,除非使用单引号,否则必须对其进行转义.这是有关引用和转义规则以及常见陷阱的资源: http://wiki.bash- hackers.org/syntax/quoting
FWIW, the issue with the backslashes is that |
has special meaning to bash and needs to be escaped unless it is in single quotes. Here is a resource on quoting and escaping rules and the common pitfalls: http://wiki.bash-hackers.org/syntax/quoting
这篇关于grep中的转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!