没有显示路径/文件的grep:line

时间:2013-10-16 14:55:39

标签: linux unix grep find

你如何grep并且只返回匹配的行?即结果中省略了路径/文件名。

在这种情况下,我想查看当前目录中的所有.bar文件,搜索术语FOO

find . -name '*.bar' -exec grep -Hn FOO {} \;

3 个答案:

答案 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.