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 7 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:18 PM

On 4/8/2008 9:57 AM, RC 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

No, never do that. Among other reasons, you just can't do it if your
script
grows moderately large and you want to save it in a file to execute using
awk
-f. Just set an awk variable based on the value of the shell variable and
use
that awk variable in your awk script:

    awk -F: -v myvar="$myvariable" '$1 ~ myvar' myfile.txt

See question 24 in the comp.unix.shell FAQ
(http://cfaj.freeshell.org/shell/cus-faq-2.html#24)
for more details.

	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:31:10 CDT 2008.