Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing more than one variable from the shell to AWK? 1

Status
Not open for further replies.

fusi0n

Technical User
Nov 1, 2006
10
US
Hi!

I'm having some problems getting AWK to use more than one variable from the shell. In a long bit of code I have the snippet:

suf=00"$i"
molwsuf="$molname"_"$suf"_hbond
h=`cat $molwsuf`
gawk '/'"$molname"_"$suf"'/ {print '"$i"', $2, $4, $6, '"$h"'}' $logfile >> $filename

Where i is an integer that increases by one for every while [ $i -le 9 ] loop. h is either 'no' or 'bond', as defined by a previous condition which then outputs no or bond to the file $molwsuf.

Everything works fine, except for the '"$h"' part. The output I get is:

0 7.51 -2.71 1.93
1 6.79 -1.95 2.26
2 6.70 -1.74 2.17
3 6.63 -2.44 2.31
4 6.56 -2.82 2.63
5 6.44 -3.10 2.68
6 6.23 -2.84 2.60
7 6.09 -2.90 2.66
8 5.95 -3.76 2.27
9 5.68 -4.15 2.78

Which is missing the $h variable (another column at the end). When I echo the gawk line, I get:

gawk '/'c5_nod_r_ou_oz_ar_009'/ {print '9', , , , 'no'}' R500log >> c5_nod_r_ou_oz_ar_tab.log

So the variable is being passed correctly, it's just not being printed (?).

Is there a limit to variables being passed to a single awk strand?

I have tried this using 'X' and 'o' instead of 'no' and 'bond', moving the '"$h"' to various places in the string, and using '"$hb"' instead.

What's going on? Why can't I use more than the one variable? Any ideas on how to get this to correctly print the string?

Thanks so much!
 
My apologies for the incorrect thread title. I'm still very new to this and thought I was having problems passing more than one 'shell' variable into my awk string.

What would a more appropriate summary of the problem be? Any ideas on how to fix it?

Thanks!
 
Hi

Code:
[gray]# the shell transforms this :[/gray]
gawk '/'"$molname"_"$suf"'/ {print '"$i"', $2, $4, $6, '"$h"'}' $logfile >> $filename

[gray]# into this :[/gray]
gawk '/'???_???'/ {print 0, $2, $4, $6, [red]no[/red]}' $logfile >> $filename
        [red]this looks like a variable name ^^[/red]

[gray]# so provide double quotes ( " ) for it :[/gray]
gawk '/'"$molname"_"$suf"'/ {print '"$i"', $2, $4, $6, [red]"[/red]'"$h"'[red]"[/red]}' $logfile >> $filename

Feherke.
 
Fantastic! That worked like a charm.

Thank you very much for the fix! And thank you for explaining what the problem was.
 
And what about this ?
gawk "/${molname}_$suf/ {print $i, \$2, \$4, \$6, \"$h\"}" $logfile >> $filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top