PDA

View Full Version : Shell Script Help




abruk
Apr 13, 2007, 03:25 PM
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.



johnee
Apr 13, 2007, 03:30 PM
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.