Skip to content

Latest commit

 

History

History
49 lines (33 loc) · 1.36 KB

dont-do-grep-wc-l.md

File metadata and controls

49 lines (33 loc) · 1.36 KB

Don't do grep | wc -l

Source: Useless Use of Cat Award

There is actually a whole class of "Useless Use of (something) | grep (something) | (something)" problems but this one usually manifests itself in scripts riddled by useless backticks and pretzel logic.

Anything that looks like:

something | grep '..*' | wc -l

can usually be rewritten like something along the lines of

something | grep -c .   # Notice that . is better than '..*'

or even (if all we want to do is check whether something produced any non-empty output lines)

something | grep . >/dev/null && ...

(or grep -q if your grep has that).

Addendum

grep -c can actually solve a large class of problems that grep | wc -l can't.

If what interests you is the count for each of a group of files, then the only way to do it with grep | wc -l is to put a loop round it. So where I had this:

grep -c "^~h" [A-Z]*/hmm[39]/newMacros

the naive solution using wc -l would have been...

for f in [A-Z]*/hmm[39]/newMacros; do
    # or worse, for f in `ls [A-Z]*/hmm[39]/newMacros` ...
    echo -n "$f:"
    # so that we know which file's results we're looking at
    grep "^~h" "$f" | wc -l
    # gag me with a spoon
done

...and notice that we also had to fiddle to get the output in a convenient form.