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.


In reply to Re: sum of counts from unix grep -c by jmcnamara
in thread sum of counts from unix grep -c by sacked

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.