你如何 grep 并且只返回匹配的行?即结果中省略了路径/文件名。
在这种情况下,我想查看当前目录中的所有 .bar 文件,搜索术语 FOO
find . -name '*.bar' -exec grep -Hn FOO {} \;
最佳答案
无需查找
。如果您只是在特定目录中寻找模式,这就足够了:
grep -hn FOO /your/path/*.bar
其中-h
是隐藏文件名的参数,如man grep
:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
请注意,您正在使用
-H, --with-filename
Print the file name for each match. This is the default when there is more than one file to search.
https://stackoverflow.com/questions/19406761/