Count the number of lines found by grep

I was able to put the answer together with help from this question. The program "wc" program counts newlines, words and byte counts. The "-l" option specifies that the number of lines is desired. For my application, the following worked nicely to count the number of instances of "somePattern":

$grep -r "somePattern" | wc -l

At least with GNU tools:

grep -rcZ "some_pattern" | awk -F'\0' '{s+=$NF}END{print s}'

This is likely superior in speed compared to wc -l.

It also works for files with newline in name.