Escaping a parenthesis in grep/ack

牧云@^-^@ 提交于 2019-12-31 17:54:29

问题


I want to look for the string "methodname(", but I am unable to escape the "(". How can I get

grep methodname( *

or

ack-grep methodname( *

to work?


回答1:


There's two things interpreting the (: the shell, and ack-grep.

You can use '', "", or \ to escape the ( from the shell, e.g.

grep 'methodname(' *
grep "methodname(" *
grep methodname\( *

grep uses a basic regular expression language by default, so ( isn't special. (It would be if you used egrep or grep -E or grep -P.)

On the other hand, ack-grep takes Perl regular expressions as input, in which ( is also special, so you'll have to escape that too.

ack-grep 'methodname\(' *
ack-grep "methodname\\(" *
ack-grep methodname\\\( *
ack-grep 'methodname[(]' *
ack-grep "methodname[(]" *
ack-grep methodname\[\(\] *



回答2:


Try adding a \ before the (.

Small demo:

$ cat file
bar
methodname(
foo
$ grep -n methodname\( file
2:methodname(
$ 

Enclosing the pattern in single or double quotes also works:

$ grep -n 'methodname(' file
2:methodname(
$ grep -n "methodname(" file
2:methodname(
$ 


来源:https://stackoverflow.com/questions/4761242/escaping-a-parenthesis-in-grep-ack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!