[nycbug-talk] useless use of awk? how to fix?

Jan Schaumann jschauma at netmeister.org
Thu May 3 21:44:04 EDT 2012


Jesse Callaway <bonsaime at gmail.com> wrote:
> This is OBVIOUSLY an rhel/vmware related example, but check it out...
> 
> rpm -qa | awk 'BEGIN {x=0} /^vmware-tools/ {x++; pkgs[x] = $1} END{
> print "yum -y remove \\"; for (y = 1; y < x; y++) print pkgs[y] " \\";
> print ";"}' | awk -F '-[[:digit:]]' '{print $1}'

Whenever somebody asks about how to improve or fix something, my first
question is "what is it you want to do".  (I can read your code, but the
code only does what you told it to do, not what you _want_ it to do,
which may be different. ;-)

Either way, in this case, I'd ask why you want to store things in an
array first anyway if all you're doing is iterate over it lateron.  Just
print out the lines as you encounter them, which makes it a bit more
readable.

Not sure if this helps you at all. :-)

-Jan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 478 bytes
Desc: not available
URL: <https://lists.nycbug.org:8443/pipermail/talk/attachments/20120503/c63e546d/attachment.bin>


More information about the talk mailing list