Talk About Network

Google


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: Pattern mat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 14 Topic 2115 of 2345
Post > Topic >>

Re: Pattern matching a Shell variable inside awk!!

by Janis Papanagnou <Janis_Papanagnou@[EMAIL PROTECTED] > Dec 26, 2007 at 07:20 PM

fazlin wrote:
> On Dec 26, 8:15 pm, Janis Papanagnou <Janis_Papanag...@[EMAIL PROTECTED]
>
> wrote:
> 
>>fazlin wrote:
>>
>>>Hello All,
>>
>>>I have an input file "myfile" and a shell variable "VAR" whose
>>>contents are shown below:
>>
>>># cat myfile
>>>Hello World
>>>Hello Universe
>>># echo $VAR
>>>World
>>
>>>I need to process all lines in "myfile" which contains pattern "World"
>>>using awk. It works if i do as shown below:
>>
>>># awk '/World/ { print $0 }' myfile
>>
>>>But i could not use VAR to do the same operation. I tried the
>>>following even knowing that these will not work:
>>
>>># awk '/$VAR/ { print $0 }' myfile
>>
>>>and
>>
>>># awk -v lvar=$VAR '/lvar/ { print $0 }' myfile
>>
>>>and
>>
>>># awk -v lvar=$VAR 'lvar { print $0 }' myfile
>>
>>   awk -v lvar=$VAR '$0~lvar { print $0 }' myfile
>>
>>or, since print $0 is the default, just
>>
>>   awk -v lvar=$VAR '$0~lvar' myfile
>>
>>(Though, there are some caveats using patterns in variables, if
>>they contain special pattern meta characters and not only text.)
>>
>>Janis
>>
>>
>>
>>
>>
>>
>>>Please let me know how to match the contents of VAR in awk.
>>
>>>Thanks in advance,
>>>Fazlin- Hide quoted text -
>>
>>- Show quoted text -- Hide quoted text -
>>
>>- Show quoted text -
> 
> 
> Thanks for your reply. I have another query. Is there any way by which
> i can use lvar inside // ?

(That's more of an OS/shell question than awk...)

You can do that, but I would not recommend it. You would have to fiddle
around with quoting, as in

   awk '/'$var'/{print "Hello "$1}'

It's also possible to embed (on Unix) the awk program in double quotes
and let the shell expand the variables

   awk "/$var/{print \"Hello \"\$1}"

but this requires a lot of fiddling with escapes since in awk one uses
double quotes for strings, and you have to prevent that awk parameters
like $0 or $1 will also be expanded by the shell.

Janis
 




 14 Posts in Topic:
Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 06:12:22 
Re: Pattern matching a Shell variable inside awk!!
Janis Papanagnou <Jani  2007-12-26 16:15:52 
Re: Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 09:26:07 
Re: Pattern matching a Shell variable inside awk!!
Janis Papanagnou <Jani  2007-12-26 19:20:53 
Re: Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 12:46:42 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-27 07:54:50 
Re: Pattern matching a Shell variable inside awk!!
gazelle@[EMAIL PROTECTED]  2007-12-26 21:36:48 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-26 16:18:02 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-27 10:05:25 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-26 23:52:10 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-28 08:00:25 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-27 17:57:59 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-28 12:54:06 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2008-01-02 09:50:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:50:46 CDT 2008.