Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abruk

macrumors newbie
Original poster
I'm trying to write a shell script that accepts a pattern and any number of file names and then counts the number of occurrences of the pattern in the listed files. A pattern can occur more than once on the same line. The only return value is one number that is the number of times the pattern occurs in all the files. I was wondering if anyone had any tips on how to do write this script. Thanks.
 
I'm trying to write a shell script that accepts a pattern and any number of file names and then counts the number of occurrences of the pattern in the listed files. A pattern can occur more than once on the same line. The only return value is one number that is the number of times the pattern occurs in all the files. I was wondering if anyone had any tips on how to do write this script. Thanks.

I know general unix scripting, but not sure if mac osx has grep, i assume it does.

you could use grep -c <phrase> <file> which will return the number of occurences of <phrase> in <file> . just loop for every file you pass in and accumulate the number in a var.

actually, i just read your requirement again, and grep -c won't do what you want if the phrase appears more than once on a line.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.