bash 如何使用 grep -e 匹配部分单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47261438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:35:33  来源:igfitidea点击:

How to use grep -e to match partial words?

linuxbashgrep

提问by Flo Bayer

If you use grep like

如果您使用 grep 之类的

grep -rnw '/your/path/to/search/' -e 'n getFoo'

grep wouldn't find a file that contains "function getFoo()".

grep 找不到包含“function getFoo()”的文件。

If you only search for 'getFoo' or else 'function getFoo', grep will find the file that contains the function.

如果您只搜索 'getFoo' 或 'function getFoo',grep 将找到包含该函数的文件。

So what's the best way to find a file containing part(s) of a string?

那么找到包含字符串部分的文件的最佳方法是什么?

Thanks in advance!

提前致谢!

回答by nyronium

You should remove the -woption which tells grep to only match whole words.

您应该删除-w告诉 grep 只匹配整个单词的选项。

grep -rn '/your/path/to/search/' -e 'n getFoo'

will also search in between word boundaries.

还将在单词边界之间进行搜索。

回答by sjsam

The -wflag causes whole word matches so it clearly what you don't need.

-w标志会导致整个单词匹配,因此很清楚您不需要什么。

So what's the best way to find a file containing part(s) of a string?

那么找到包含字符串部分的文件的最佳方法是什么?

shopt -s globstar
grep -li 'string to search for' /path/to/search/**
shopt -u globstar