I like to use the unix command grep -c (print a count of matches) on multiple files at a time, but it displays the count for each file. I want a total count, so I call on Perl. Pipe the output of grep -c PATTERN file1 file2 ... to this snippet.

update: don't use this, use blakem's better solution below.
perl -anF: -e '$sum += $F[-1] }{ print $sum.$/'

Replies are listed 'Best First'.
Re: sum of counts from unix grep -c
by blakem (Monsignor) on Jan 18, 2002 at 04:45 UTC
    I like it.... You could do the whole thing in perl though, giving you more powerful regexes...
    % perl -ne '$sum += /pattern/}{print $sum,$/' file1 file2 file3
    Or, perhaps something as evil as:
    % perl -lpe '$\+=/pattern/}{$\.=$/' file1 file2 file3

    -Blake

      Great! Thanks very much. I completely overlooked the fact that I could use Perl for counting.

      --sacked
Re: sum of counts from unix grep -c
by jmcnamara (Monsignor) on Jan 18, 2002 at 14:04 UTC

    If you use grep -hc then only the counts will be displayed and you can do away with the -F.

    You could then calculate the total using the following: ;-)     grep -hc pattern files | perl -lpe '${_{_}}+=+${_}}{${_}+=+${_{_}}'

    This is shown somewhat more clearly in Sum of the first column numbers.

    However, I don't believe that ever nail should be hit with a Perl hammer. This works just as well:     cat files | grep -c pattern

    Update: This is not a useless use of cat as stated below.

    The cat command is used here in its intended role to concatenate files into a single stream. In this way the pattern count is a single total as sacked required.

    The phrase useless use of cat might lead you to believe that the cat is unnecessary and that the following statements are equivalent:

    cat files | grep -hc pattern grep -hc pattern files

    They aren't.(1) Not unless you take files to mean a single file.

    This would be a very narrow reading given the context of the original posting, the context of the reply and the fact that "files" is plural.

    In the CB merlyn said that it would have been better written as follows:     cat file1 file2 ... | grep -hc pattern

    This is certainly valid. However, the command as posted hardly merits an unqualified and misleading useless use of cat.

    (1) To see the difference try something like the following:

    cat *.h | grep -hc define grep -hc define *.h
    --
    John.

      % cat tmp1 llama camel % cat tmp2 camel gecko % cat tmp1 tmp2 | grep -hc camel 2 % grep -hc camel tmp1 tmp2 1 1 %
      Not a useless use of cat after all. :) cat concatenates the files into a single stream, which is exactly what one needs in order to get a total count over all the files.