15

I'm trying to show only unique filenames when I grep a certain string. Currently I'm getting multiple results with the same filename if a certain string appear several times inside a file.

for example:

If I have a string "string12345" and it appears 3 times in several lines inside filename1.txt and appear 3 times in several lines inside filename2.txt as well when I use *grep 'string12345' .txt it shows 3 occurrences of filename1.txt and filename2.txt

Now what I'm trying to achieve is to show only 1 occurrence of filename1.txt and filename2.txt. Thank you in advance.

vic-rattlehead
  • 337
  • 1
  • 3
  • 14

1 Answers1

23

use the -l flag.

test.txt:

Hello, World!
Hello, World!

Grep search:

$ grep ./ -re "Hello"
./test.txt:Hello, World!
./test.txt:Hello, World!
$ grep ./ -re "Hello" -l
./test.txt

From the manual:

-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.
Eric Bringley
  • 1,366
  • 1
  • 12
  • 21