How to escape parenthesis in grep

白昼怎懂夜的黑 提交于 2019-12-09 04:59:19

问题


I want to grep for a function call 'init()' in all JavaScript files in a directory. How do I do this using grep?

Particularly, how do I escape parenthesis, ()?


回答1:


It depends. If you use regular grep, you don't escape:

echo '(foo)'|grep '(fo*)'

You actually have to escape if you want to use the parentheses as grouping.

If you use extended regular expressions, you do escape:

echo '(foo)'|grep -E '\(fo*\)'



回答2:


If you want to search for exactly the string "init()" then use fgrep "init()" or grep -F "init()".

Both of these will do fixed string matching, i.e. will treat the pattern as a plain string to search for and not as a regex. I believe it is also faster than doing a regex search.




回答3:


$ echo "init()" | grep -Erin 'init\([^)]*\)'
1:init()

$ echo "init(test)" | grep -Erin 'init\([^)]*\)'
1:init(test)

$ echo "initwhat" | grep -Erin 'init\([^)]*\)'



回答4:


Move to your root directory (if you are aware where the JavaScript files are). Then do the following.

grep 'init()' *.js


来源:https://stackoverflow.com/questions/3673594/how-to-escape-parenthesis-in-grep

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