Grep的行为会有所不同,具体取决于我在正则表达式中使用哪种引号。我似乎无法清楚地了解为什么会这样。这是问题的示例:
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 \(
我只是假设有某种适当的方法将正则表达式用单引号/双引号引起来。有什么帮助吗?
FWIW,
grep --version
返回grep (GNU grep) 2.5.1
最佳答案
包含自变量的命令行在执行之前由外壳程序处理。您可以使用echo查看外壳程序的作用:
$ 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
因此,不带引号的反斜杠将被删除,从而使“(”成为grep的普通字符(默认情况下,grep使用基本正则表达式,使用-E使grep使用扩展正则表达式)。
关于regex - 使用grep时引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3008423/