Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show unique filename only on command grep result [duplicate]

Tags:

grep

unix

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.

like image 285
vic-rattlehead Avatar asked Mar 09 '18 09:03

vic-rattlehead


1 Answers

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.
like image 183
Eric Bringley Avatar answered Oct 07 '22 16:10

Eric Bringley