Thanks for this. I know nothing about awk so this is entirely new territory. I guess I was hoping the for loop would work and I wouldn't have to use something like awk.
–
karavanJan 15 '13 at 16:39

Of course the for loop can work. Try this for pat in 'pat1' 'pat2' 'pat3'; do echo $pat $(grep -E -rc "$pat" $1); done. Depending on the version of grep you're using, you may need to tweak this command a (tiny) bit. I only mentioned awk so that you loop only once over the subdirs (in case there are too many) and you want to control the output or add some calculation. The awk script I wrote is somewhat incomplete but should give you an idea of how it works. Try to read it I bet you'd find out awk isn't as hard as it's made out to be :)
–
rahmuJan 15 '13 at 16:40

Trying to figure out why the for loop from @rahmu above doesn't work. I did: for pat in 'text1' 'text2' 'text3'; do echo $(grep -E -rc '$pat' $1); done' The result is something like this for every file in the directory (there are thousands): "/data/dir1/file:0". What I am expecting is "text1=10, text2=20, text3=32". Thoughts?
–
karavanJan 15 '13 at 18:52

The following script loops over the pattern and prints the total count of matches for each pattern. It searches under the directory whose name is passed as an argument, or under the current directory if you don't pass an argument.

That's exactly it! Thank you! I would vote up your answer but I don't have enough reputation points :). Can you explain this ${1:-.} a little? I understand that it is the variable from the arguement, but what does :- do?
–
karavanJan 16 '13 at 16:58

@karavan: This is the syntax for defining a default value. If $1 is empty (if no command line arguments are supplied), then use to value .. man bash and a quick search of :- would explain it best.
–
rahmuJan 19 '13 at 15:08