Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > awk and $variable

awk and $variable
Thread Tools
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Dec 27, 2002, 08:50 AM
 
How can I use $variable IN SIDE `awk'?

The idea is that first


set variable1=one
set veriable2=two


and then use these variables something like this [it didn't quite work like this... maybe because of missing escape characters... but I can't figure it out...]


awk '{if ($1 ~ `echo $variable1`) print "`echo $variable2`"}' myfile.txt

     
Dedicated MacNNer
Join Date: Sep 2001
Location: San Jose CA
Status: Offline
Reply With Quote
Dec 27, 2002, 01:49 PM
 
Try this:

awk '{if ($1 ~ '$variable1') print "'$variable2'"}' myfile.txt

When you use the single quote, the shell treats all the special characters
such as $ and ` as ordinary characters. By using multiple sets of single quotes
you can get around this.

BTW, you don't need the echo command since the shell will automatically
substitute the value of a variable when you use the $ special character.

HTH,

--ranga
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Dec 28, 2002, 05:56 AM
 
Doesn't really work...

However, adding about 10 `for' and `if' I managed to get my original try to work
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:45 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2