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 8 of 10 Topic 2218 of 2236
Post > Topic >>

Re: How to pass UNIX variable into awk?

by pk <pk@[EMAIL PROTECTED] > Apr 8, 2008 at 05:19 PM

RC wrote:

> myvariable="hello"
> 
> cat my file, if first field match "hello", then print whole line

awk '$1 ~ /hello/' myfile.txt

> 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 did this more than 10 years ago, now I forget the trick.
> If you know, please tell me again.
> Thank Q in advance!

awk -v myvar="hello" '$1 ~ myvar' myfile.txt

-- 
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.




 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:36:33 CDT 2008.