canito at dalan.us wrote:
> In the process of writing a script which I would like to count the
> number matched patterns (command line parameters).

I think your approach can be refined. The `grep' family of commands
would certainly work here, but not necessarily in the way you're
envisioning. There are two basic forms of using grep

    grep -c PATTERN FILE
    COMMAND_OUTPUT | grep -c PATTERN

The `-c' option gives you count, but only in terms of lines that match,
not necessarily in terms of every matching instance.

    cat << EOMSG | grep -c ONE
    ONE, TWO, THREE
    ONE, ONE
    ONE
    TWO, ONE, ONE
    EOMSG

will result in an answer of 4 rather than 6. Remember, counting lines
rather than instances.

If instances on a single line are important, such as with argument
strings, you could do some pre-formatting tricks to still use grep.

    echo "ONE TWO ONE THREE ONE FOUR" | sed -e 's/[[:space:]]/\n/g' | \
    	 grep -c ONE

The `sed' command will replace all white space with newlines, allowing
you to count lines to find matches in your arguments.

There are other ways to skin this cat (pun intended) with shell-fu, but
these are simple and easy to grep... (I can't stop.)

Chad

-- 
Chad Walstrom <chewie at wookimus.net>
http://runswithd6s.blogspot.net/