简体   繁体   English

使用grep时引用?

[英]Quotes when using grep?

Grep acts differently depending on what kind of quotes I surround the regex with. Grep的行为取决于我围绕正则表达式的引号类型。 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 FWIW, grep --version返回grep (GNU grep) 2.5.1

The command line including the arguments is processed by the shell before it is executed. 包含参数的命令行在执行之前由shell处理。 You can use echo to see what the shell does: 您可以使用echo来查看shell的功能:

$ 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的普通字符(grep默认使用基本的正则表达式,使用-E使grep使用扩展的正则表达式)。

In order: 为了:

grep -e show( test.txt

does not work, because the shell interprets the ( as special, a parenthesis, not just a character, and can't find the closing ) . 不起作用,因为shell解释(特殊的,括号,而不仅仅是一个字符,并且无法找到结束)

These both work: 这两个都有效:

grep -e 'show(' test.txt
grep -e "show(" test.txt

because the shell treats the quoted text as just text, and passes it to grep. 因为shell将引用的文本视为文本,并将其传递给grep。

These do not work: 这些不起作用:

grep -e 'show\(' test.txt
grep -e "show\(" test.txt

because the shell passes show\\( to grep, grep sees \\( as special, a parenthesis, not just a character, and can't find the closing \\) . 因为shell传递show\\(到grep,grep看到\\(作为特殊的,括号,而不仅仅是一个字符,并且找不到结束\\)

The quotes change what grep sees. 引号改变了grep看到的内容。 The backslash (\\) in the un-quoted form is processed by the shell, which treats characters after the backslash as special. 未引用形式的反斜杠(\\)由shell处理,它将反斜杠后的字符视为特殊字符。 This happens before grep gets the parameter. 这在grep获取参数之前发生。 grep sees show( . When the quotes (single or double) are used, the shell interprets them as "leave the contents alone", thus grep sees show\\( and the \\( characters have meaning in grep and it is looking for the closing parenthesis - \\). grep看show( 。当使用引号(单引号或双引号)时,shell将它们解释为“单独保留内容”,因此grep看到show \\(\\(字符在grep中有意义,它正在寻找结束)括号 - \\)。

BTW: Single and double quote handling is different in how the shell handles shell variables, but there are no shell variables in your example. BTW:单引号和双引号处理在shell处理shell变量的方式上有所不同,但在您的示例中没有shell变量。

I do not believe it is grep that is behaving differently, it is the shell. 我不相信它是表现不同的grep,它是shell。 I'm assuming you are using bash 我假设你正在使用bash

http://www.faqs.org/docs/bashman/bashref_8.html http://www.faqs.org/docs/bashman/bashref_8.html

Basically the quoted versions are behaving differently on the slash, depending on the quoting mechanism. 基本上,引用的版本在斜杠上的行为有所不同,具体取决于引用机制。

Both of the quoted examples would have worked without the slash. 两个引用的例子都可以在没有斜线的情况下工作。 For the first one, the shell would escape the ( and pass in just show( to the grep for the pattern. 对于第一个,shell将逃脱(并传入刚显示(到模式的grep)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM