Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: awk tutoria...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 2222 of 2236
Post > Topic >>

Re: awk tutorial confusion

by Ed Morton <morton@[EMAIL PROTECTED] > Apr 16, 2008 at 05:12 PM

On 4/16/2008 2:13 PM, Bruce Barnett wrote:
> Ed Morton <morton@[EMAIL PROTECTED]
> writes:
> 
> 
>>Only one problem: the script doesn't work. ... You need to turn off the
quoting
>>when the variable is seen. This can be done by ending the quoting, and
>>restarting it after the variable:
>>
>>#!/bin/sh
>>column=$1
>>awk '{print $'$column'}'
>>----------------------
>>
>>which is the worst possible way to pass the value of a shell
>>variable to an awk program (see
>>http://cfaj.freeshell.org/shell/cus-faq-2.html#24
for the right way).
> 
> 
> I have always felt that it is essential to understand this particular
> mechanism because it solves many shell programming problems. Your
> suggestion can *only* be used with AWK and cannot be applied to sed,
> grep, bash, etc.  I prefer to teach the more versatile approach.

I understand that may initially seem reasonable if you're coming from a
"how do
I use shell" perspective, but it doesn't when you're coming from a "how do
I use
awk" perspective and it's a lot like disavowing using EREs in awk since
they
aren't supported in standard sed, or disavowing the use of backreferences
to
matching RE segments in sed since they aren't supported in awk or grep,
or....

In other words, given some alternatives, we shouldn't be telling people to
do
the worst possible thing in one tool just because some other tools with
inferior
capabilities are limitted to ONLY doing that bad thing.

> If you continue reading, I give 4 more variations of this
> "Column" function, including version 5, which is:
> 
> -------------
> #!/bin/sh
> awk '{print $c}' c=${1:-1}
> ------------
> 
> (it defaults to printing column #1 if no arguments are given.)

Thats fine as an example of a shell script, but it isn't a good example of
an
awk script since it relies on the OS to do some processing. I don't know
what
the Windows syntax is to accomplish the same thing but the general awk
script
would be the much wordier:

    awk -v c="$1" 'BEGIN{ c=(c==""?1:c) }{ print $c }'

Note that by setting c using "-v" the variable is available in the BEGIN
section
 as well as during the main body processing which is why you should
generally
use "-v" for setting variables rather than mixing the setting of variables
in
with the file arguments.

Obviously, if I was writing a shell script I'd really use:

   awk -v c="{1:-1}" '{print $c}'

but I wouldn't even be using this as an example in an "_AWK_" tutorial.

> When I wrote this tutorial in 1994, I described the only versions that
> worked in the old "classic" AWK.

sub(/classic/,"broken")   ;-).

> I admit that my tutorial is not up to date.
> I try to do what I can.

I do understand and I expect there's a lot of good information there but
the
section I read is very wrong for people using modern awks such as gawk,
nawk, or
/usr/xpg4/bin/awk so I'm not sure who would be an appropriate target
audience
for it any more.

> But you do make a good point so I added a link to the FAQ you references

> to cover the newer AWKs.

OK. You could also add something like "if your awk doesn't support -v,
you're
using old, broken awk and for your own sanity must get a new awk".

	Ed.




 11 Posts in Topic:
awk tutorial confusion
r <inpost@[EMAIL PROTE  2008-04-14 01:53:41 
Re: awk tutorial confusion
Ed Morton <morton@[EMA  2008-04-14 06:00:16 
Re: awk tutorial confusion
Ed Morton <morton@[EMA  2008-04-14 06:21:55 
Re: awk tutorial confusion
Bruce Barnett <spamhat  2008-04-16 15:13:08 
Re: awk tutorial confusion
Ed Morton <morton@[EMA  2008-04-16 17:12:31 
Re: awk tutorial confusion
Bruce Barnett <spamhat  2008-04-17 20:11:28 
Re: awk tutorial confusion
Ed Morton <morton@[EMA  2008-04-17 23:48:23 
OT: Re: awk tutorial confusion
Ed Morton <morton@[EMA  2008-04-17 00:22:15 
Re: awk tutorial confusion
Bruce Barnett <spamhat  2008-04-16 14:28:50 
Re: awk tutorial confusion
r <inpost@[EMAIL PROTE  2008-04-16 14:42:52 
Re: awk tutorial confusion
Bruce Barnett <spamhat  2008-04-17 20:02:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 11:30:10 CDT 2008.