Searching in Files Using awk and grep
by Marshall on May 18, 2005
Sometimes it is handy to have a list of files which contain a certain string (or a regex). You can combine grep and awk in the following manner:
grep -H "string to search for" *.txt | awk -F: '{ print $1 }'
The -H option in grep makes it display the file name before the text that matched the string. The -F option for awk lets you specify the field delimiter, in this case, :.
2 comments
Easier:
grep -l “string to search for” *.txt
From grep(1):
-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.
(And faster!)
by Ed Catmur on May 20, 2005 at 10:41 pm. #
Thanks a lot. Much easier to remember too!
by Marshall on May 22, 2005 at 6:16 pm. #