in reply to sum of counts from unix grep -c


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.

Replies are listed 'Best First'.
Re: Re: sum of counts from unix grep -c
by chipmunk (Parson) on Jan 18, 2002 at 19:23 UTC
    % 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.
        "files" is plural.
        No indication whatsoever, except that "files" is plural.
Re: Re: sum of counts from unix grep -c
by merlyn (Sage) on Jan 18, 2002 at 18:36 UTC