How to add printf statement in awk command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add printf statement in awk command?
# 1  
Old 08-12-2014
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
Code:
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[i] ":" AMT[i]
                 for (i=1;i<=c;i++) print " count " ITM[i] ":" CNT[i]
                 print "total amount:" TOTA
                 print "total count:" TOTC}
        ' file

the above gives the output of total amount as
Code:
total amount:1234.50

is it possibel to add the printf statement like
Code:
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...
# 2  
Old 08-12-2014
A straight translation of your script changing the print statement you have to the printf you seem to want is:
Code:
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[i] ":" AMT[i]
		for (i=1;i<=c;i++) print " count " ITM[i] ":" CNT[i]
		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:
Code:
	' TOTA=12345.67 /dev/null

and it produced the output I expected:
Code:
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.)
# 3  
Old 08-12-2014
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
Code:
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
Code:
amount 0124:12345.67
count   0124 : 8
amount 0125 : 0
count    0125 : 0
total amount:12345.67
total count:8

expected ouput should be
Code:
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
Code:
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[i] " amount:" AMT[i]
                 for (i=2;i<=NR;i++) print ITM[i] " count:" CNT[i]
                 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

Last edited by hemanthsaikumar; 08-12-2014 at 03:22 AM..
# 4  
Old 08-12-2014
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???

Last edited by Don Cragun; 08-12-2014 at 04:49 AM.. Reason: Add notes about differences between supplied code output and output shown.
# 5  
Old 08-12-2014
I didn't realize this same topic was being actively discussed in another thread. This thread is closed!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using df -g command with awk to get SQL statement

Hi Gurus... good day; currently I trying to run the df -g command with awk to get to convert in SQL statement, but I have some errors; df -g | awk '{print "This is the FileSystem: " $NF, " This is LV: "$1, "This is SIZE: "$2, "This is FREE: " $3, "This is the USED% "$4}' This on AIX... (3 Replies)
Discussion started by: wcastibl
3 Replies

2. Shell Programming and Scripting

Printf statement for currency conversion

hi all, I had my script as a=qw b=rter c=fdfd curency=1000 printf"${curency} $a $b $c" > filename can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?(i.e for any number) (14 Replies)
Discussion started by: hemanthsaikumar
14 Replies

3. Shell Programming and Scripting

Combining awk printf and print strftime command

I have a lines like below, captured from rrdtool fetch command, 1395295200 2.0629986254e+06 7.4634784967e+05 1395297000 2.0198121616e+06 6.8658888903e+05 1395298800 1.8787141122e+06 6.7482866452e+05 1395300600 1.7586118678e+06 6.7867977653e+05 1395302400 1.8222762151e+06 7.1301678859e+05I'm... (3 Replies)
Discussion started by: rk4k
3 Replies

4. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

5. Shell Programming and Scripting

creating printf statement using user arguments

I am writing a script in bash and want to perform the operation I check number of arguments and make a print statement with the passes arguments If I pass 3 arguments I will do printf "$frmt" "$1" "$2" "$3"If I have 4 arguments I do printf "$frmt" "$1" "$2" "$3" "$4"etc (4 Replies)
Discussion started by: kristinu
4 Replies

6. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

7. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 Replies

8. Shell Programming and Scripting

Printf statement

The printf statement pay_amount=$(printf "%013.3f" "$4") working perfectly at one path(xxx/home/rsh) and showing error (printf: 216.000: invalid number) at another path(/opt/xxxx/xxxx). what will be the reason? thanks in advance (4 Replies)
Discussion started by: reeta_shri
4 Replies

9. Shell Programming and Scripting

How to convert unix command into Awk statement

Hi all, How can i use the below unix command in AWK . Can any one please suggest me how i can use. sed -e "s/which first.sh/which \$0/g" $shell > $shell.sal where $0=current program name(say current.sh) $shell=second.sh (1 Reply)
Discussion started by: krishna_gnv
1 Replies

10. Shell Programming and Scripting

awk command for INSERT statement

Hi, I sometimes bulk upload data in oracle. The problem is that I sometimes get an INSERT statemnt like this: INSERT INTO ALL_USER_HOTSPOT_DETAILS (USR_LOGIN,USR_LASTNAME,USR_FIRSTNAME,USR_EMAIL, PROPERTYNR) VALUES ('SABRDAG','D'AGOS','SABRINA','sabrina_d'agos@sheraton.com',70) I... (4 Replies)
Discussion started by: nattynatty
4 Replies
Login or Register to Ask a Question