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: How to pass...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 10 Topic 2218 of 2236
Post > Topic >>

Re: How to pass UNIX variable into awk?

by Ed Morton <morton@[EMAIL PROTECTED] > Apr 8, 2008 at 07:00 PM

On 4/8/2008 6:39 PM, Kenny McCormack wrote:
> In article <47FBFE15.2010500@[EMAIL PROTECTED]
>,
> Ed Morton  <morton@[EMAIL PROTECTED]
> wrote:
> ...
> 
>>Yes, it is:
>>
>>$ var="#"
>>$ awk 'BEGIN{ print '"$var"' }'
>>awk: cmd. line:1: BEGIN{ print # }
>>awk: cmd. line:1:              ^ syntax error
>>$ awk -v var="$var" 'BEGIN{ print var }'
>>#
> 
> 
> You make it entirely too easy for me:
> 
> sh-3.1$ var="#"
> sh-3.1$ action="print"
> sh-3.1$ gawk 'BEGIN {'$action' "'$var'"}'
> #
> sh-3.1$ 

$ var="hello
world"
$ awk 'BEGIN{ print "'$var'" }'
awk: BEGIN{ print "hello
awk:              ^ unterminated string
$ awk -v var="$var" 'BEGIN{ print var }'
hello
world

> Now, to be fair, I'm not saying there isn't some total corner case that
> involves the variable having the value "rm -rf /;echo foo on you | Mail
> prez@[EMAIL PROTECTED]
" or something like that.  I think the shell nuts may
> well come up with something, but I do think that for all normal cases,
> the risk is the same using either the "hopping back and forth" method
> and using the "var=" form.

It's not unusual for a shell variable to contain a newline or some other
character that doesn't work when trying this hopping back and forth
between
shell and awk approach, and then that approach just can't be used at all
if your
awk script is in a file to be invoked with awk -f.

> But I don't really care about any of that; the point is that for
> real-world day-to-day use, this is method is good enough.  You are, of
> course, free to disagree with that.
> 

OK, I disagree with that. Just use a variable. It works for all cases and
it's
just a few extra charaters of typing.

	Ed.




 10 Posts in Topic:
How to pass UNIX variable into awk?
RC <raymond.chui@[EMAI  2008-04-08 10:43:05 
Re: How to pass UNIX variable into awk?
RC <raymond.chui@[EMAI  2008-04-08 10:57:32 
Re: How to pass UNIX variable into awk?
gazelle@[EMAIL PROTECTED]  2008-04-08 15:00:24 
Re: How to pass UNIX variable into awk?
Ed Morton <morton@[EMA  2008-04-08 18:21:57 
Re: How to pass UNIX variable into awk?
gazelle@[EMAIL PROTECTED]  2008-04-08 23:39:23 
Re: How to pass UNIX variable into awk?
Ed Morton <morton@[EMA  2008-04-08 19:00:06 
Re: How to pass UNIX variable into awk?
Ed Morton <morton@[EMA  2008-04-08 18:18:21 
Re: How to pass UNIX variable into awk?
pk <pk@[EMAIL PROTECTE  2008-04-08 17:19:25 
Re: How to pass UNIX variable into awk?
Janis <janis_papanagno  2008-04-08 08:09:54 
Re: How to pass UNIX variable into awk?
pk <pk@[EMAIL PROTECTE  2008-04-08 17:31:07 

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 23:29:25 CDT 2008.