仅在命令 grep 结果上显示唯一文件名

vic*_*ead 5 unix grep

当我grep某个字符串时,我试图只显示唯一的文件名。目前,如果某个字符串在文件中多次出现,我会得到多个具有相同文件名的结果。

例如:

如果我有一个字符串“string12345”并且它在 filename1.txt 中的几行中出现 3 次,并且在 filename2.txt 中的几行中出现 3 次,当我使用 *grep 'string12345' .txt 时,它会显示 3 次出现 filename1。 txt 和 filename2.txt

现在我想要实现的是只显示 1 次出现的 filename1.txt 和 filename2.txt。先感谢您。

Eri*_*ley 10

使用-l旗帜。

测试.txt:

Hello, World!
Hello, World!
Run Code Online (Sandbox Code Playgroud)

搜索:

$ grep ./ -re "Hello"
./test.txt:Hello, World!
./test.txt:Hello, World!
$ grep ./ -re "Hello" -l
./test.txt
Run Code Online (Sandbox Code Playgroud)

从手册:

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