grep -l and grep -ln

7
根据grep的手册,
-l, --files-with-matches
          Suppress normal output; instead print the  name  of  each  input
          file  from  which  output would normally have been printed.  The
          scanning will stop on the first match.

grep -l,这个命令在发现匹配项时会输出包含匹配项的文件名,看起来很好理解。

但是,当我使用grep -ln时,它会输出匹配项的每一行。

是否意味着grep -l只找到第一个匹配项就停止扫描,并且grep -ln忽略了-l标志?

1个回答

8
这些选项是不兼容的。如果您想显示每个文件中第一个匹配项(仅限第一个匹配项)的行号,请使用grep -Hnm 1

-H, --with-filename
打印每个匹配项所在的文件名。

-n, --line-number
在输出的每一行前面加上它在输入文件中的行号。

-m NUM, --max-count=NUM
在匹配了NUM行后停止读取文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接