 |
 |
Variable expansion in awk
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I want to use variables inside an awk command as follows:
Code:
awk '$1==$MYVARIABLE {print $2}'
so that if the file contains
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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"
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|