使用grep时的引号? [英] Quotes when using grep?

查看:171
本文介绍了使用grep时的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆