Shell Scripts
Shell Scripts
Posted May 3, 2010 19:11 UTC (Mon) by nescafe (subscriber, #45063)In reply to: Shell Scripts by HelloWorld
Parent article: Poettering: Rethinking PID 1
Posted May 3, 2010 19:20 UTC (Mon)
by HelloWorld (guest, #56129)
[Link] (12 responses)
Posted May 3, 2010 20:20 UTC (Mon)
by nescafe (subscriber, #45063)
[Link] (11 responses)
People do things like
for foo in 'find /path -name someglob'
over several thousand files and then complain about how slow shell code is.
Seriously.
Posted May 3, 2010 21:00 UTC (Mon)
by HelloWorld (guest, #56129)
[Link] (2 responses)
Posted May 6, 2010 12:27 UTC (Thu)
by nescafe (subscriber, #45063)
[Link]
Posted May 10, 2010 17:36 UTC (Mon)
by jschrod (subscriber, #1646)
[Link]
Posted May 5, 2010 13:54 UTC (Wed)
by paulj (subscriber, #341)
[Link] (7 responses)
You'd be much better off with:
E.g. Your shell example could be done with:
or using GNU find's built-in xargs-ish feature (when was that added?):
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.
Posted May 5, 2010 14:05 UTC (Wed)
by johill (subscriber, #25196)
[Link] (1 responses)
sed 's/foo/\0/;t;d'
Posted May 5, 2010 16:30 UTC (Wed)
by martinfick (subscriber, #4455)
[Link]
Posted May 5, 2010 14:46 UTC (Wed)
by k8to (guest, #15413)
[Link] (1 responses)
Posted May 5, 2010 15:07 UTC (Wed)
by paulj (subscriber, #341)
[Link]
Posted May 5, 2010 16:28 UTC (Wed)
by fredi@lwn (subscriber, #65912)
[Link] (2 responses)
find | grep | xargs cut ...
or similar. Though the -exec on your last example for what i recall is slower than:
find /foo -name $GLOB -print0 | xargs -0 SOMECOMMAND
That because with -exec you start on each found entry another process while xargs passes all entriess to the same process if they fit in the command line max length. Hope i gave the idea & sorry for my bad english.
Posted May 5, 2010 18:05 UTC (Wed)
by paulj (subscriber, #341)
[Link] (1 responses)
Posted May 6, 2010 15:48 UTC (Thu)
by fredi@lwn (subscriber, #65912)
[Link]
Shell Scripts
Shell Scripts
if [ `cat $foo |grep ^bar &>/dev/null; echo $?` == 0 ]; then
echo `cat $foo |grep ^bar |awk '{print $2}'`; fi
done
Shell Scripts
Shell Scripts
Shell Scripts
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:
Shell Scripts
grep XYZ | awk ... '{ ... }'
awk ... '/XYZ/ { .... }'
find /path -name someglob | xargs awk '/^bar/ { print $2 }'
find /path -name someglob -exec awk '/^bar/ { print $2 }' {} +
Shell Scripts
Shell Scripts
Shell Scripts
Shell Scripts
Shell Scripts
Shell Scripts
Shell Scripts