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

Jesse Callaway bonsaime at gmail.com
Thu May 3 22:37:44 EDT 2012


On Thu, May 3, 2012 at 9:44 PM, Jan Schaumann <jschauma at netmeister.org> wrote:
> 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
>
> _______________________________________________
> talk mailing list
> talk at lists.nycbug.org
> http://lists.nycbug.org/mailman/listinfo/talk
>

That's a totally good question, and it's probably the #1 thing someone
should think about before writing any script - no matter how trivial.

The reason why it's done like this is because it the package manager
enjoys uninstalling all of this at once so it doesn't bother me with
inter-dependencies when I want to remove the whole lot.

I'm more wondering if there is some other way of doing this BEGIN {};
{} ....; END {} construct that I should be using instead of invoking
another instance of awk for the FS stuff.

This is not a data muncher, so the extra exec isn't a huge deal, but
all the same it would be good to get some insight from the peanut
gallery.

-- 
-jesse



More information about the talk mailing list