Free tech support / small logo


Another approach to working with "awk"

A reader writes in with the following note:
I've just picked up your book Wicked Cool Shell Scripts, and there are a lot of nifty things in it. I too have accumulated a lot of things generally in small scripts or more frequently in bourne/bash functions. I was particularly tickled to see a lot of very similar code, one such that jumped out at me was your gmk function in one script (I called mine kmg).

Would you be interested in some constructive criticism?

In script #52 Killing Processes by Name, I think that instead of:
  awk "/ $1$/{ print \$2 }"
you might consider using
  awk "$NF=="$1" { print \$2 }"

That way, you're not dependent on whitespace.


Dave's Answer: You're right, this is a better approach to the problem, but you're not solving the same problem I am. :-)

What I'm getting is the last field if and only if it's prefaced by a space, whereas you're ignoring the leading space and looking just to see if the last field matches "$1".

Where might this differ?

When the output is a single word on a line by itself: with your conditional it could match, whereas with mine it couldn't.

Nonetheless, I am happy to hear about smarter and more logical ways to solve problems that I talk about in my book, so please do feel free to send any further thoughts in!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments
Rather amazingly, there are no comments on this article yet.

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.