LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-19-2007, 12:21 PM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
passing a shell variable into awk - syntax for correct interpretation?


Hi.

Say I define a variable within a bash script, and want to pass the various values it can take on to awk as shown below...

Code:
var="a b c d"
Code:
for i in $var
 do
  awk '$1 == $i {print $1,$2,$3}' some.file
As it is the $i is not being interpreted as I want it to be...can anyone help me with the correct escape syntax?

Thanks
 
Old 06-19-2007, 12:46 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

You need to use awk's -v var=var option. I.e:
Code:
#!/bin/bash

var="a b c d"

for i in $var
 do
  awk -v i="$i" '$1 == i {print $1,$2,$3}' some.file
done
Hope this helps.
 
Old 06-19-2007, 01:04 PM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Ok, here's the scoop.
  1. You don't need to use awk's -v var=var option. You can, and the previous poster was correct in this, but it wouldn't hurt to learn bash's rules for quoting and escaping/interpreting characters.
  2. Starting with bash and leaving awk out of this for a moment, anything between single quotes will be taken literally by bash: no backslash escapes (backslashes will be taken as simple backslashes), no dollar sign interpretation (dollar signs will be taken as simple dollar signs), and so on.
  3. For bash, anything between double quotes will be interpreted for backslash escapes and dollar signs.
  4. If you want a backslash or dollar sign within a double quoted string to be taken literally as though it were in a single quoted string, precede that character with a backslash.
  5. Moving on to awk, you want to compare the $1 (the first field on each data line as seen by awk with a constant value. That constant value isn't really constant; it's in $i. But you want bash to present it to awk as a constant. If awk is to see this as a constant, it needs to be surrounded by double quotes.

In the following script, the awk command needs to be double quoted, because you want bash to interpret the $i, substituting the appropriate value (a, b, c, or d). But you want the $1 to appear literally as $1 for awk, so escape the $ in the $1 to make that happen. To put double quotes within a double quoted string, escape each of those quotes. Also escape the $1, $2, and $3 later in the command, because awk needs to see those dollar signs exactly as they are, with no interpretation by bash.

I've preceded the bash/awk stuff with a simple demonstration of single and double quoting in bash.

Code:
#!/bin/bash

var="a b c d"

for i in $var
do
  echo "$i" '$i' "\$i" '\$i'
done

for i in $var
do
  awk "\$1 == \"$i\" {print \$1,\$2,\$3}" <<EOD
a aa aaa
e ee eee
i ii iii
b bb bbb
f ff fff
j jj jjj
c cc ccc
g gg ggg
k kk kkk
d dd ddd
h hh hhh
l ll lll
EOD
  echo =====================
done
The output looks like this:

Code:
a $i $i \$i
b $i $i \$i
c $i $i \$i
d $i $i \$i
a aa aaa
=====================
b bb bbb
=====================
c cc ccc
=====================
d dd ddd
=====================
Hope this helps.
 
Old 06-19-2007, 01:13 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Good and solid advice, although it has a possible downside: You do need to start escaping (a lot of?) characters.

awk -v i="$i" '$1 == i {print $1,$2,$3}'

vs

awk "\$1 == \"$i\" {print \$1,\$2,\$3}"

Both work, so that makes it a personal choice.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting 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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a function and passing a variable the correct way ForYouAndI.com Programming 2 06-07-2007 10:44 PM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 11:15 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 03:18 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM


All times are GMT -5. The time now is 01:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration