grep
If you're only interested in the names of the files that contain a search string 1 time you can use grep
with its -l
switch to do this.
Example
Say I have 2 files full of numbers.
$ seq 100 > sample1.txt
$ seq 100 > sample2.txt
Now if I search that file for occurrences of the string "10".
$ grep -l 10 sample*.txt
sample1.txt
sample2.txt
It will only return the files that contain a match 1 time, even if there are multiple lines that match. As proof, if I take the -l
switch out:
$ grep 10 sample*.txt
sample1.txt:10
sample1.txt:100
sample2.txt:10
sample2.txt:100
pcregrep
If you want to search for patterns across multiple lines you can use pcregrep
along with its -M
switch, for multi-line.
$ pcregrep -M "11[\n,]*.*12" sample*
sample1.txt:11
12
sample2.txt:11
12