Building Complex Command Lines Build simple commands into full-fledged paragraphs for complex (but meaningful) reports [Discuss (4) | Link to this hack]
Comments on this hack
Showing messages 1 through 4 of 4.
Gratuitous use of wc...
2003-10-27 17:03:25
anonymous2
[View]
As a previous post pointed out, rather than using 'wc -l', you can tack the '-c' flag on grep and have it count. You can also use the count flag on uniq - after a sort, 'uniq -c' will remove duplicate entries while couting them, and give you the number of each occurance of unique text.
Gratuitous use of wc...
2003-10-27 17:42:24
rflicken
[View]
Some people find my gratuitous nature one of my most compelling attributes. That and my modesty. ;)
Seriously, yes, by all means use the -c switch wherever applicable. Part of the beauty and power of Unix is that there is always more than one way to do it, but the "right" way is different for just about everyone.
using the for x loop could produce erroneous report
2003-07-31 19:41:56
anonymous2
[View]
The grep in the for loop looks for all lines containing the file name string. If that string appears in other log file entries it would then be included in the count--inflating it. The revision offered by the prior posting eliminates this problem in addition to making it a cleaner, faster solution.
Useless use of wc -l
2003-04-14 15:04:11
aristotle
[View]
As you can read in detail at http://www.sektorn.mooo.com/era/unix/award.html#wc , this hack as presented by the book suffers badly from "useless use of wc -l" syndrome.
grep "File does not exist:" error_log | wc -l
is better written as
grep -c "File does not exist:" error_log
Also, it's much easier on memory and CPU to replace "sort | uniq" by the equivalent "sort -u" which doesn't fist sort all occurences, only to then throw away dupes, but throws away dupes as soon as it encounters them.
If one is interested in the number of occurences, instead of writing a severly redundant "for" loop that greps the error_log once per unique name for the missing files, a quick look at the uniq(1) manpage shows that it understands the same "-c" option as grep(1). The entire "for" loop cruft can therefor be replaced with a simple
grep "File does not exist:" error_log | awk '{print $13}' | sort | uniq -c
Note that this produces the frequency as the first thing on the line, so to generate a report we needn't even tell sort(1) which field to pick.
grep "File does not exist:" error_log | awk '{print $13}' | sort | uniq -c | sort -rns | head -20