Shell Script Help

Discussion in 'Mac Programming' started by abruk, Apr 13, 2007.

  1. abruk macrumors newbie

    Apr 13, 2007
    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.
  2. johnee macrumors 6502a


    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.

Share This Page