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!

awk script and quoting problem 1

Status
Not open for further replies.

ls62

Programmer
Oct 15, 2001
178
US
Hi,

I have a shell script that uses awk and I build the awk command from variables. I'm having a problem because some of the data contains the " character and is messing up my quoting. Can someone help me and tell me how to resolve the quoting issue or a better way to do this. Here's my script and a sample of output. BAsically I have two files ... FILEA contains some fields that I supplie to the awk command to find matches in FILEB. Some of my 'prod' variable data contains the '"' character.

Script:

#!/bin/sh
while read line
do
ven=`echo $line | cut -c 122-124`
prod=`echo $line | cut -c 32-39`
clr=`echo $line | cut -c 21-30`
q=`echo $line | cut -c 54-62`

awk -e 'BEGIN { FS = "," }
$16 == "'$ven'" && $5 == "'$prod'" && $4 == "'$clr'" {
if ( "'$q'" != $7 ) { print $16,$5,$4,$7,"'$q'" }
}
' <FILEA

done <FILEB

---- ERROR OUTPUT -----
Note that the $5 == $prod where $prod is 60&quot;GRGTE. The &quot; messes up my quoting.

awk: newline in string { $4 == ...
at line 2 of program << BEGIN { FS = &quot;,&quot; }
... >>
context is
$16 == &quot;ZEL&quot; && $5 == &quot;60&quot;GRGTE&quot; && $4 == &quot;WHITE &quot; { >>>
<<<
awk: Syntax error
at line 3 of program << BEGIN { FS = &quot;,&quot; }
... >>
1 extra }
awk: bailing out
at line 3 of program << BEGIN { FS = &quot;,&quot; }
... >>

-----------------
Thanks for any help. Maybe there's a better way to do this, if so I'll take any suggestions.

LEE
 
Have you tried something like this ?
#!/bin/sh
while read line
do
ven=`echo $line | cut -c 122-124`
prod=`echo $line | cut -c 32-39`
clr=`echo $line | cut -c 21-30`
q=`echo $line | cut -c 54-62`

awk -v ven=&quot;$ven&quot; -v prod=&quot;$prod&quot; -v clr=&quot;$clr&quot; -v q=&quot;$q&quot; -e '
BEGIN { FS = &quot;,&quot; }
$16 == ven && $5 == prod && $4 == clr {
if ( q != $7 ) { print $16,$5,$4,$7,q }
}' <FILEA
done <FILEB

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
PHV,

Ahh.. very good. Didn't even realize that the '-v' option was available. Works perfect now. Thanks for your quick response and help.

Lee
 
Another way to do the work, reading FILEA and FILEB only once.
[tt]
awk '
FNR == 1 { FileNum += 1 }
FileNum == 1 {
clr = substr($0,21,10);
prod = substr($0,32,8);
q = substr($0,54,9);
ven = substr($0,122,3);
qval[clr prod ven] = q;
}
FileNum == 2 {
q = qval[$4 $5 $16];
if (q != $7)
print $16,$5,$4,$7,q;
}
' FILEB FILEA
[/tt]


Jean Pierre.
 
aigles,

I like your idea too. I guessing it loads one file into a array and looks up from there, right? The filea is pretty big, so would there be a memory issue. There's about 1500 records.

Lee
 
FILEB is load into an array (the index of array is clr+prod+ven and the value q).
There is no memory problem with FILEA which isn't keep in memory.

Jean Pierre.
 
Fields in FILEB may contains leading and/or trailing spaces (we get them with 'cut' or 'substr').
Fields in FILEA are read by 'awk' which removes leading and trailing spaces (field separators).
So comparing fields from FILEB to fields from FILEA may not work. You must remove leading/trailing spaces from FILEB fields.

[tt]
ven=`echo $line | cut -c 122-124 | sed -e 's/^ *//' -e 's/ *$//'`
[tt]
or in awk
[tt]
ven = substr($0,122,3); sub(/^ */,&quot;&quot;,ven) ; sub(/ *$/,'',ven);
[tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top