How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output....
below is my code :
Code Credits :RudiC

awk -F, 'NF==2  {next}
                {ITM[++c]=$1
                 AMT[c]=$2+0
                 CNT[c]=$3+0
                 TOTA+=$2
                 TOTC+=$3}
         END    {for (i=1;i<=c;i++) print "amount " ITM ":" AMT
                 for (i=1;i<=c;i++) print " count " ITM ":" CNT
                 print "total amount:" TOTA
                 print "total count:" TOTC}
        ' file

the above gives the output of total amount as

total amount:1234.50

is it possibel to add the printf statement like

printf "%'.2f" TOTA

to get the total amount:1,234.50...?
while iam trying to add this to the awk command iam getting the following error
unexpected EOF while looking for matching `" '
Please guide me on this .. is it possibel to add the above printf statement to the above awk command..? if not please suggest me some solution...

A straight translation of your script changing the print statement you have to the printf you seem to want is:

awk -F, -v sq="'" '
	NF==2 {	next}
	{	ITM[++c]=$1
		AMT[c]=$2+0
		CNT[c]=$3+0
		TOTA+=$2
		TOTC+=$3}
        END {	for (i=1;i<=c;i++) print "amount " ITM ":" AMT
		for (i=1;i<=c;i++) print " count " ITM ":" CNT
		printf("total amount:%" sq ".2f\n", TOTA)
		print "total count:" TOTC}
	' file

But, you have specific what OS you're using and you have given us any idea of what is in file , so I have no idea if this will work for you.

Trying just to test the printf to see if it gives you something reasonable, I tested it by replacing the last line of your script with:

	' TOTA=12345.67 /dev/null

and it produced the output I expected:

total amount:12,345.67
total count:

If you want to try this on a Solaris/SunOS system, change awk to /usr/xgp4/bin/awk or /usr/xpg6/bin/awk . (It might also with with nawk , but I'm not sure if it supports the printf ' formatting flag.)

Hi Don,

for the below code can i have a possibility of doing the same for the amount ...? printf statement for the amount field..?

my input file will be

12345.67,8
0124,12345.67,8
0125,0,0

after running the script iam getting the ouput as
my output file will be like

amount 0124:12345.67
count   0124 : 8
amount 0125 : 0
count    0125 : 0
total amount:12345.67
total count:8

expected ouput should be

amount 0124:12,345.67
count 0124 : 8
amount 0125 : 0
count 0125 : 0
total amount:12,345.67
total count:8

below is my modified script

awk -F, 'NF<3   {next}
                {ITM[NR]=$1
                 AMT[NR]=$2
                 CNT[NR]=$3
                 TOTA+=$2
                 TOTC+=$3}
         END    {for (i=2;i<=NR;i++) print ITM " amount:" AMT
                 for (i=2;i<=NR;i++) print ITM " count:" CNT
                 print "total amount:" TOTA 
                 print "total count:" TOTC} 
        ' file

according to the script you provided it will append comma for total amount ..is der any possibility like it will add comma for the amount 0124,amount 0125 fields too...?

thanks for the help

regards,
Vasa Saikumar

So, let me see if I understand this. You ask to have TOTA printed with commas, I provide you with code that will do that, and you throw away the code I provided that would do that.

And after showing you how to print TOTA the way you said you wanted it, you aren't even willing to try something similar to print TOTA and AMT[i].

Why did you throw away the code I suggested? Can't you try to adapt the printf example I gave you to print AMT [i]in the format you want?

And the script you provided doesn't even come close to producing the output you said it produces. (The spacing is different and the order of the output lines is different.)

What OS are you using that produces the output you showed us from that awk script???

I didn't realize this same topic was being actively discussed in another thread. This thread is closed!