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!

Parsing Error in AWK -- printf shell varaibles

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
I have got the printf() error in AWK (AIX Unix). It seems that I will never get that AWK work with my user-defined shell variables.
Please help!!!

I was trying to print some fileds from input.dat into an output file. The other fields of the output file are from Unix shell variables.

### input.dat
ZYRTEC 5 MG TABLET |ZYRTEC 5 MG TABLET F|F0307|
ZYRTEC-D TABLET |ZYRTEC-D TABLET F|F0308|
ACEON 4 MG TABLET |ACEON 4 MG TABLET P|P0001|

### Output
F0307|EN|ZYRTEC 5 MG TABLET F|dbl|04/24/2006 04:54:04 PM|
F0308|EN|ZYRTEC-D TABLET F|dbl|04/24/2006 04:54:04 PM|
P0001|EN|ACEON 4 MG TABLET P|dbl|04/24/2006 04:54:04 PM|

Here is the scipt I used, which generated the AWK parseing error as following.
#!/bin/ksh

user=`whoami`
tmstamp=`date +"%m/%d/%G %r"`

echo "user: <$user>"
echo "tmstamp: <$tmstamp>"

#awk -F'|' '{printf "%s|%2s|%s|\n", $3, "EN", $2}' input.dat
awk -F'|' '{printf "%s|%2s|%s|%-16s|%-24s|\n", $3, "EN", $2, '$user', '$tmstamp'}' input.dat


/home/users/dbl/PPM/database> gen_code.awk
user: <dbl>
tmstamp: <04/24/2006 04:58:16 PM>
Syntax Error The source line is 1.
The error context is
{printf "%s|%2s|%s|%-16s|%-24s|\n", $3, "EN", $2, dbl, >>> 04/24/2006 <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.


 
awk -F'|' '{printf "%s|%2s|%s|%-16s|%-24s|\n", $3, "EN", $2, [!]"[/!]'$user'[!]"[/!], [!]"[/!]'[!]"[/!]$tmstamp[!]"[/!]'[!]"[/!]}' input.dat

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, PHV. You're awesome!

Could you please tell me the difference between this one:

awk -F'|' '{printf "%s|%2s|%s|%-16s|%-24s|\n", $3, "EN", $2, "'"$user"'", "'"$tmstamp"'"}' input.dat

and the one I posted before:

awk -F'|' 'BEGIN{start='$st'}{printf "%s|%s|F%04d|\n",$1,$2,start++}'
 
You have to understand 2 things:
1) how to write literal and numeric constants in an awk program
2) what is your shell's quoting mechanism.

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