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 4 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 06:21 PM

On 4/8/2008 10:00 AM, Kenny McCormack wrote:
> In article <ftg14s$392$1@[EMAIL PROTECTED]
>,
> RC  <raymond.chui@[EMAIL PROTECTED]
> wrote:
> 
>>RC wrote:
>>
>>>myvariable="hello"
>>>
>>>cat my file, if first field match "hello", then print whole line
>>>
>>>I tried
>>>
>>>cat myfile.txt | awk -F: '$1 ~ /$myvariable/ {print $0}'
>>>
>>>but doesn't work, I also tried double $ like $1 ~ /$$myvariable/
>>>still no luck.
>>
>>I found the answer from
>>http://www.faqs.org/faqs/unix-faq/faq/part3/section-12.html
>>
>>cat myfile.txt | awk -F: '$1 ~ /'"$myvariable"'/ {print $0}'
>>
>>Where '" are single quote and double quotes
>>      "' are double quotes and single quote
> 
> 
> Good for you.
> 
> Now, of course, the nitpickers will come along and point out:
> 	1) UUOC
> 	2) UUO$0
> 	3) In the nanny-state, you aren't supposed to do it this way.
> 	    They have this wacky idea that it is safer/better to use -v.
> 	    Hint: It isn't.
> 

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 }'
#

Regards,

	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:04:00 CDT 2008.