Always Display Filename with grep
The easiest way to accomplish this is if you have a relatively recent GNU version of grep
. Then the -H
version is available to you:
printf "This\nis\a\ntest" > /tmp/grep.test
grep -H 'is' /tmp/grep.test
/tmp/grep.test:This
/tmp/grep.test:is
But if the -H
isn't available to you (I'm looking at your SunOS/Solaris) there's still hope, and it's nearly as easy:
printf "This\nis\a\ntest" > /tmp/grep.test
grep 'is' /dev/null /tmp/grep.test
/tmp/grep.test:This
/tmp/grep.test:is
By giving grep
/dev/null
(which it sees as a zero length file) in addition to the file you'd like to search grep
sees two files and will report back the filename of the file you'd like to search.