使用grep时的引号? [英] Quotes when using grep?
问题描述
hamiltont $ grep -e show\(test.txt
variable。 show();
variable.show(a);
variable.show(abc,132);
variableshow();
hamiltont $ grep -eshow\ test.txt
grep:Unmatched(or \(
hamiltont $ grep -e'show\''test.txt
grep:Unmatched(or \(
我只是假设有一些正确的方法用单/双引号括起正则表达式。 >
FWIW, grep --version
返回 grep(GNU grep)2.5.1
包含参数的命令行在执行前由shell处理,您可以使用 echo 来查看shell的功能:
$ echo grep -e show\(test.txt
grep -e show(test.txt
$ echo grep -eshow\(test.txt
grep -e show\(test.txt
$ ech o grep -e'show \('test.txt
grep -e show \(test.txt
因此,如果没有引号,反斜杠会被删除,使得(grep的普通字符(默认情况下grep使用基本的正则表达式,使用-E使grep使用扩展正则表达式)。
Grep acts differently depending on what kind of quotes I surround the regex with. I can't seem to get a clear understanding of why this is. Here is an example of the problem:
hamiltont$ grep -e show\( test.txt
variable.show();
variable.show(a);
variable.show(abc, 132);
variableshow();
hamiltont$ grep -e "show\(" test.txt
grep: Unmatched ( or \(
hamiltont$ grep -e 'show\(' test.txt
grep: Unmatched ( or \(
I am just assuming there is some proper way to enclose the regex with single/double quotes. Any help?
FWIW, grep --version
returns grep (GNU grep) 2.5.1
The command line including the arguments is processed by the shell before it is executed. You can use echo to see what the shell does:
$ echo grep -e show\( test.txt
grep -e show( test.txt
$ echo grep -e "show\(" test.txt
grep -e show\( test.txt
$ echo grep -e 'show\(' test.txt
grep -e show\( test.txt
So without quotes the backslash gets removed making the "(" a normal character for grep (grep uses basic regex by default, use -E to make grep use extended regex).
这篇关于使用grep时的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!