在 grep 中省略文件名

在 grep 中省略文件名

我正在从多个文件中 grep 一个字符串,但有一个不想要的副作用,就是文件名出现在输出前面。如何仅使用 grep 来抑制文件名输出?

  $ grep -i lp lpNet* 
    lpNet:This was printed via the internet using the lp command.
    lpNet:I believe lp doesnt care what the device is. 
    lpNet1:This was printed via the internet using the lp command.
    lpNet1:I believe lp doesnt care what the device is. 
    lpNet2:This was printed via the internet using the lp command.
    lpNet2:I believe lp doesnt care what the device is. 
    lpNet3:This was printed via the internet using the lp command.
    lpNet3:I believe lp doesnt care what the device is. 

我已经使用 cat lpNet* | grep lp 解决了这个问题,我只是想知道是否有更有效的路径来达到同样的效果

答案1

默认行为是在给定多个文件参数时打印文件名 - 为了抑制这种情况,您可以添加-h或 --no-filename 选项

来自Output Line Prefix Controlgrep 手册页的部分:

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

相关内容