awk sum giving incorrect value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sum giving incorrect value
# 1  
Old 05-19-2012
awk sum giving incorrect value

Code:
 
cat T|awk -v format=$format '{ SUM += $1} END { printf format,SUM}'

the file T has below data

Code:
 
usghrt45tf:hrguat:/home/hrguat $ cat T
-1363000.00123456789
-95000.00789456123
-986000.0045612378
-594000.0015978
-368939.54159753258415
-310259.0578945612
-133197.37123456789
-12475383.16789456123
-2812276.0312345678

the output of above command is "-19138055.18514395505190" with format set to

format='%22.14f'

the actual result of the sum should be "-19138055.18514395762415"
It looks like the unix is approximating the last 6 digits in the scale. Could any one please help ?
# 2  
Old 05-19-2012
Try:
Code:
sum=0;while read n; do sum=`echo "scale=14;$sum+$n"| bc`; done < T; echo $sum

# 3  
Old 05-19-2012
Code:
sum=0;while read n; do sum=`echo "scale=14;$sum+$n"| bc`; done < T; echo $sum

this works but the only point which holds me back is, the above should iterate for each record and might be time consuming when there huge number of records.
# 4  
Old 05-19-2012
Try:
Code:
awk '{$1=$1}1' OFS=+ RS=X infile | bc

or to specify a precise precision:

Code:
awk 'BEGIN{print "scale=14; "} {$1=$1}1' OFS=+ RS= infile


Last edited by Scrutinizer; 05-19-2012 at 09:32 AM..
# 5  
Old 05-19-2012
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '{$1=$1}1' OFS=\+ RS=X infile | bc

The result is same as cat on the file name, guess something is missing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk incorrect format

I was wondering whether anyone has any idea what is happening here. I'm using simple code to compare 2 tab delimited files based on column 1 values. If the column1 value of file1 exists in file2, then I'm to print the column4 value in file2 in column3 of file1. Here is my code: 1st I have to... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk command gives incorrect result?

Hi All, I am looking to filter out filesystems which are greter than a specific value. I use the command df -h | awk '$4 >=70.00 {print $4,$5}' But this results out as below, which also gives for lower values. 9% /u01 86% /home 8% /u01/data 82% /install 70% /u01/app Looks... (3 Replies)
Discussion started by: jjoy
3 Replies

3. Shell Programming and Scripting

Df -h | awk - output incorrect matching

Running solaris 9, on issuing the follwing command df -h | awk '$5 > 45 {print}' Filesystems with utilisation > 45% are being displayed as well as those between 5 and-9%!!! (3 Replies)
Discussion started by: squrcles
3 Replies

4. Shell Programming and Scripting

wc -L giving incorrect length of longest line

Running below line gives 3957 as length of longest line in file 20121119_SRMNotes_init.dat awk ' { if ( length > 3950 ) { x = length } }END{ print x }' 20121119_SRMNotes_init.dat While wc -L 20121119_SRMNotes_init.dat gives output as 4329. Why is there a difference between these two commands.... (2 Replies)
Discussion started by: Satish Mantha
2 Replies

5. Shell Programming and Scripting

Disk Monitoring shell script giving incorrect information

Hi All, OS: Linux 86x64 bits Red Hat Linux I get the email alert for the following when Alert condition is set for 30: /dev/sda1 99M 21M 74M 22% /boot -> Below 30%(Should not get the email alert) Expected output as per E-Mail alert: /dev/sda3 20G ... (2 Replies)
Discussion started by: a1_win
2 Replies

6. Shell Programming and Scripting

awk : deleting specific incorrect lines

Hello friends, I searched in forums for similar threads but what I want is to have a single awk code to perform followings; I have a big log file going like this; ... 7450494 1724465 -47 003A98B710C0 7450492 1724461 -69 003A98B710C0 7450488 1724459 001DA1915B70 trafo_14:3 7450482... (5 Replies)
Discussion started by: enes71
5 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

awk to extract incorrect fixed length records

I have a number of unix text files containing fixed-length records (normal unix linefeed terminator) where I need to find odd records which are an incorrect length. The data is not validated and records can contain odd backslash characters and control characters which makes them awkward to process... (2 Replies)
Discussion started by: methyl
2 Replies

9. Shell Programming and Scripting

Merge lines in a file with Awk - incorrect output

Hi, I would like: FastEthernet0/0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 0 interface resets Serial1/0:0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0... (14 Replies)
Discussion started by: mv652
14 Replies

10. Shell Programming and Scripting

Awk incorrect data.

I am using the following command: nawk -F"," 'NR==FNR {a=$1;next} a {print a,$1,$2,$3}' file1 file2 I am getting 40 records output. But when i import file1 and file2 in MS Access i get 140 records. And i know 140 is correct count. Appreciate your help on correcting the above script (5 Replies)
Discussion started by: pinnacle
5 Replies
Login or Register to Ask a Question