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 > Variable expansion in awk

Variable expansion in awk
Thread Tools
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Apr 17, 2005, 12:22 AM
 
I want to use variables inside an awk command as follows:
Code:
awk '$1==$MYVARIABLE {print $2}'
so that if the file contains
Code:
foo bar jim bob
and $MYVARIABLE happens to be "foo" then it returns "bar," but if it's "jim" then I get "bob" back. As is, the above syntax gives an error, and if I enclose $MYVARIABLE in quotes then it just searches for the literal string "$MYVARIABLE." Is there any way around this?

And yes, this has to be with awk. I'm trying to replace my current grep | cut solution, as awk is faster.
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Apr 17, 2005, 10:10 AM
 
Nevermind, I figured it out. For posterity, the correct syntax would be:
Code:
awk "\$1==\"$MYVARIABLE\" {print \$2}"
     
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status: Offline
Reply With Quote
Apr 17, 2005, 10:36 AM
 
Originally Posted by wataru
Nevermind, I figured it out. For posterity, the correct syntax would be:
Code:
awk "\$1==\"$MYVARIABLE\" {print \$2}"
So it had to do with the single quotes versus the double quotes that you ended up using. The single quotes weren't allowing for variable expansion, eh?
"In Nomine Patris, Et Fili, Et Spiritus Sancti"

     
Junior Member
Join Date: May 2004
Status: Offline
Reply With Quote
Apr 17, 2005, 12:03 PM
 
Originally Posted by [APi]TheMan
So it had to do with the single quotes versus the double quotes that you ended up using. The single quotes weren't allowing for variable expansion, eh?
yeah. most shells i think work the same way here. single quotes dont expand anything not even escapes. double quotes expand all kinds of things.
     
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status: Offline
Reply With Quote
Apr 17, 2005, 02:14 PM
 
Originally Posted by leira
yeah. most shells i think work the same way here. single quotes dont expand anything not even escapes. double quotes expand all kinds of things.
Yeah, single quotes are as literal as you can get, correct? What's interesting to me in that awk statement is the escaping of the double quotes inside the awk statement itself.
(Last edited by [APi]TheMan; Apr 17, 2005 at 10:02 PM. )
"In Nomine Patris, Et Fili, Et Spiritus Sancti"

     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Apr 17, 2005, 09:39 PM
 
Yes, the problem was the single-quotes. The reason for escaping the double-quotes inside the awk command is that otherwise awk doesn't receive them; they are apparently used up in ensuring that $MYVARIABLE is sent as a single string (as opposed to multiple strings in the event of whitespace). Unfortunately, this doesn't cut it, so you have to send the double-quote literals to awk by escaping them.
     
   
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 07:22 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