Try this (nextfile is a GNU extension):
awk '$1=="ice" && $2=="cream"{print FILENAME;nextfile}' file1 file2 file3
Or if you don't have GNU:
awk 'FNR==1{p=0} $1=="ice" && $2=="cream" && !p {print FILENAME;p=1}' file1 file2 file3
The FNR==1
resets the p
flag at the start of each file, and then when your criteria are met, the p
flag gets set to 1
so the filename only comes out once.
By the way, you may prefer the simplicity of this:
grep -l "^ice.*cream" file*
It is not identical, but pretty similar.
EDITED AFTER ACCEPTANCE
This is another possibility that may be more elegant. It saves the names of matching files in an array names[]
and prints the keys of the array names[]
at the end.
awk '$1=="ice" && $2=="cream" {names[FILENAME]++} END{for(i in names)print i}'