Shell Scripts
Posted May 5, 2010 13:54 UTC (Wed) by
paulj (subscriber, #341)
In reply to:
Shell Scripts by nescafe
Parent article:
Poettering: Rethinking PID 1
It's a real shame people don't know how to use AWK properly. It's a fairly capable little language. One of the common abuses is piping grep to AWK - as AWK applies regexes itself to every line[1]. Basically, if we can assume input tends not to be huge or that most the input will be acted on, then whenever you see:
grep XYZ | awk ... '{ ... }'
You'd be much better off with:
awk ... '/XYZ/ { .... }'
E.g. Your shell example could be done with:
find /path -name someglob | xargs awk '/^bar/ { print $2 }'
or using GNU find's built-in xargs-ish feature (when was that added?):
find /path -name someglob -exec awk '/^bar/ { print $2 }' {} +
This is meant more for the peanut gallery than for you ;) - I was expecting there'd be a rush to offer more optimal one-liners, strangely there hasn't been. ;)
1. Though, as Padraig Brady has shown me, beyond a certain size of file, there is a benefit to using grep to pre-filter input if you're discarding a sufficient amount of that input, as grep is much faster at processing each line than AWK.
(
Log in to post comments)