根据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会回显事件的每一行。
grep -l
是否真的意味着在找到第一个匹配项时停止并停止扫描,而grep -ln
会忽略-l
标志?
答案 0 :(得分:6)
这些选项不兼容。如果要在每个文件中显示第一个匹配(以及仅第一个匹配)的行号,请使用grep -Hnm 1
。
-H, - with-filename
打印每场比赛的文件名。-n, - line-number
使用输入文件中的行号为每行输出添加前缀。-m NUM , - max-count = NUM
在 NUM 匹配行后停止读取文件。