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 > OT: Re: awk tut...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 11 Topic 2222 of 2236
Post > Topic >>

OT: Re: awk tutorial confusion

by Ed Morton <morton@[EMAIL PROTECTED] > Apr 17, 2008 at 12:22 AM

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.

By the way, whether it's awk or sed or whatever, IIRC the right way to do
what
you're attempting in the above script is:

#!/bin/sh
column=$1
awk '{print $'"$column"'}'

i.e. you need to quote your shell variable so you get some protection from
some
of the various possible errors this approach introduces, e.g.:

$ var="hello world"

$ awk 'BEGIN{print "'$var'"}'
awk: BEGIN{print "hello
awk:             ^ unterminated string

$ echo "foo bar" | sed 's/.*/'$var'/'
sed: -e expression #1, char 10: unterminated `s' command

$ awk 'BEGIN{print "'"$var"'"}'
hello world

$ echo "foo bar" | sed 's/.*/'"$var"'/'
hello world

but of course even that only goes so far:

$ var="hello
world"

$ awk 'BEGIN{print "'"$var"'"}'
awk: BEGIN{print "hello
awk:             ^ unterminated string

$ echo "foo bar" | sed 's/.*/'"$var"'/'
sed: -e expression #1, char 10: unterminated `s' command

$ awk -v var="$var" 'BEGIN{print var}'
hello
world

The sed solution requires escaping the text in the variable.

Regards,

	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:41:47 CDT 2008.