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

All*_*mas 209 unix linux grep find

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

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

find . -name '*.bar' -exec grep -Hn FOO {} \;
Run Code Online (Sandbox Code Playgroud)

fed*_*qui 338

没必要find.如果您只是在特定目录中查找模式,这应该足够了:

grep -hn FOO /your/path/*.bar
Run Code Online (Sandbox Code Playgroud)

-h隐藏文件名的参数在哪里,如下所示man grep:

-h, - no-filename

禁止输出上的文件名前缀.当只有一个文件(或仅标准输入)要搜索时,这是默认设置.

请注意,您正在使用

-H, - with-filename

打印每个匹配的文件名.当有多个要搜索的文件时,这是默认设置.

  • 行号可以省略吗 (2认同)
  • @javadba 省略 -n 标志 (2认同)

jks*_*hah 8

只需更换-H-h.检查man grep有关选项的更多详细信息

find . -name '*.bar' -exec grep -hn FOO {} \;
Run Code Online (Sandbox Code Playgroud)


TC1*_*TC1 6

从手册页:

-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.
Run Code Online (Sandbox Code Playgroud)