Facing problem while printing accurate decimal value

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Facing problem while printing accurate decimal value
# 1  
Old 01-08-2014
Bug Facing problem while printing accurate decimal value


I have a file tp.txt having below data
CE2DD,N,5055,16.12.2013,3.114,12195.89,MVR,003388,Web::00000005055,Web Payment

and i am using below code to print the values
for var_amt_pay in `awk -F',' '{ arr[$1","$5","$8] += $6} END {for (i in arr) {print i "," arr[i] } }' tp.txt`
    code=`echo $var_amt_pay | awk -F',' '{ print $1 }'`
    cust=`echo $var_amt_pay | awk -F',' '{ print $2 }'`
    num=`echo $var_amt_pay | awk -F',' '{ print $3 }'`
    amt=`echo $var_amt_pay | awk -F',' '{ print $4 }'`
   echo "amt is :"$amt

it is giving the output as amt is :12195.9 instead of amt is :12195.89
Please help how can i get the output as 12195.89


Last edited by bartus11; 01-08-2014 at 07:37 AM.. Reason: code tag closing marker is "[/code]"
# 2  
Old 01-08-2014
Here is a way to get what you want

$ echo "CE2DD,N,5055,16.12.2013,3.114,12195.89,MVR,003388,Web::00000005055,Web Payment" | \
awk -F',' '{ arr[$1","$5","$8] += $6} END {for (i in arr) print i "," sprintf("%5.2f",arr[i])  }'

$ echo "CE2DD,N,5055,16.12.2013,3.114,12195.89,MVR,003388,Web::00000005055,Web Payment" | \
awk -F',' '{ arr[$1","$5","$8] += $6} END {for (i in arr) print i "," arr[i]}' CONVFMT="%5.2f"

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-08-2014
By the way why do you even need a for loop and several pipes to awk to do all these?

You could do the whole thing in one awk program:
awk -F, '
        BEGIN {
                CONVFMT = "%.2f"
                A[$1 FS $5 FS $8] += $6
        END {
                for ( k in A )
                        split ( k, R )
                        print "Code: " R[1]
                        print "Cust: " R[2]
                        print "Nume: " R[3]
                        print "Amnt: " A[k]
' tp.txt

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing with decimal places from last 4 digits

I have input file like below, 201424|9999|OSS|622010|RGT|00378228764 201424|8888|OM|587079|RGT|00284329675 201424|7777|OM|587076|RGT|00128671024 201424|6666|OM|581528|RGT|00113552084 Output should be like below, should add decimal (.) from last 4 digits. ... (2 Replies)
Discussion started by: vinothsekark
2 Replies

2. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

3. Shell Programming and Scripting

Trying to copy Using scp facing problem

source file is located in (elk.some.com) /export/elk2/vp141p/Somedir/dist/current/Filename.ear destination machine(191.hydc.xxx.com) /export/home/vp141p/ARCHIVE scp -p vp141p@hstst191.hydc.sbc.com:/export/elk2/vp141p/PM_Build_SBS/Build_PVT_SBS/dist/current/Filename.ear . The above code is... (5 Replies)
Discussion started by: vishwakar
5 Replies

4. Shell Programming and Scripting

problem facing in if -else condition

can u plz tell me where is the error echo enter the filename to be searched read fname if #-d $fname then echo file exists if then echo itsa directory elif then echo its readable cat $fname else echo its not readable fi else ... (1 Reply)
Discussion started by: gotam
1 Replies

5. Infrastructure Monitoring

Facing problem while configuring snmp

HI, I am facing these two errors while configuring snmp can any body guide me. vi /var/log/snmpd.log Error opening specified endpoint "udp:161" Server Exiting with code 1 i also tried bash-3.00# netstat -a | grep snm *.snmpd Idle bash-3.00# lsof -i :161 bash: lsof: command not... (2 Replies)
Discussion started by: nir1785
2 Replies

6. Solaris

Facing problem with zone

i am using this way to create zone1 and zone2 bash-2.05b# zonecfg -z zone1 zone1: No such zone configured Use 'create' to begin configuring a new zone. zonecfg:zone1> create zonecfg:zone1> set zonepath=/zone/1 zonecfg:zone1> set autoboot=true zonecfg:zone1> add net zonecfg:zone1:net>... (6 Replies)
Discussion started by: coxmanchester
6 Replies

7. Shell Programming and Scripting

Awking!! Printing decimal output is struck

Hi friends, I have a small problem with AWK. I am not able to print decimal values! :confused: below is my code: #! /bin/awk -f awk BEGIN{printf("%d",123)}; -> This prints the integer properly. x=111 awk BEGIN{printf("%d",x)}; -> This doesnt print! :( Please help me solve this. It... (4 Replies)
Discussion started by: divzz
4 Replies

8. Solaris

please help as i am facing problem with uptime

Hi I am getting the uptime output as follows 12:40am up 4 day(s), 18:29, 2 users, load average: 38.97, 36.54, 34.89 The load average is too high . I have checked the processes , but no process is taking too much cpu time Please help (3 Replies)
Discussion started by: guy009
3 Replies

9. UNIX for Dummies Questions & Answers

facing a problem in redirection

Hi, I am doing this perl script print (@line(1..15)); the lines 1 to 15 get printed... how can i redirect this to file? thanks and regards vivek.s (4 Replies)
Discussion started by: vivekshankar
4 Replies
Login or Register to Ask a Question