你如何grep并且只返回匹配的行?即结果中省略了路径/文件名。
在这种情况下,我想查看当前目录中的所有.bar文件,搜索术语FOO
find . -name '*.bar' -exec grep -Hn FOO {} \;
答案 0 :(得分:307)
无需find
。如果您只是在特定目录中查找模式,这应该足够了:
grep -hn FOO /your/path/*.bar
其中-h
是隐藏文件名的参数,从man grep
开始:
-h, - no-filename
禁止输出上的文件名前缀。这是默认值 当只有一个文件(或只有标准输入)进行搜索时。
请注意,您使用的是
-H, - with-filename
打印每场比赛的文件名。这是默认值 要搜索的文件不止一个。
答案 1 :(得分:7)
只需将-H
替换为-h
即可。查看man grep
以了解有关选项的更多详情
find . -name '*.bar' -exec grep -hn FOO {} \;
答案 2 :(得分:0)
从手册页:
-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.