in reply to sum of counts from unix grep -c
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
|
|---|
| 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 | |
by merlyn (Sage) on Jan 18, 2002 at 19:26 UTC | |
by tilly (Archbishop) on Jan 19, 2002 at 02:59 UTC | |
by thor (Priest) on Jan 28, 2002 at 11:17 UTC | |
|
Re: Re: sum of counts from unix grep -c
by merlyn (Sage) on Jan 18, 2002 at 18:36 UTC |