Summing a number column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing a number column
# 1  
Old 11-14-2013
Summing a number column

hi All,

i have a file in which only one column is there.,

test.txt
======
Code:
-900.01
-900.02
-900.03
-900.04
-900.05
-900.06
-900.07
-900.08
-900.09
900.01
900.02
900.03
900.04
900.05
900.06
900.07
900.08
900.09

i want to sum the values and get the final value.

currently i am usin the foloowing command,

Code:
Detail_Amount_Sum=0;
for i in `cat IFs_temp1.txt`
do
Detail_Amount_Sum=`echo "scale=2; $Detail_Amount_Sum + $i" | bc -l`
done
 
final_sum=$Detail_Amount_Sum;

but if i have one lakh record in the file it will take an hour to complete the for loop itself.

i need a command which will complet very fast but has to give accurate result.

my output should be in the scale of 2 like ( 900.00 or 900.01 or 0.01 or 3.27)

please send me the commands

Last edited by Scrutinizer; 11-14-2013 at 01:54 PM.. Reason: code tags
# 2  
Old 11-14-2013
It is slow because you are running bc 9,000 times for 9,000 numbers. Transform the entire stream into something you can feed into bc directly instead -- one program instead of 9,000.

For example if you transform it into lines like a+=(-5) then follows up with the last line a it will sum them all and print the resulting value.

Code:
awk 'BEGIN { print "scale=2" } { print "a+=("$1")" } END { print "a" }' inputfile | bc

Ordinarily I would use awk to sum the numbers too, but if you need infinite precision, bc is for you.

Also -- basically, you should never do for X in `cat file`, that's a dangerous use of backticks.

Last edited by Corona688; 11-14-2013 at 02:14 PM.. Reason: add 'scale'
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 11-14-2013
For a shell solution that should be faster, try something like:
Code:
{ printf "scale=2; 0"
  while read i; do
     printf "+%s " "$i"
  done
  printf "\n" 
} < IFs_temp1.txt | bc

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-14-2013
Of course, if you have access to a 1993 or later version of the Korn shell, you can do this entirely with shell built-ins:
Code:
#!/bin/ksh
Detail_Amount_Sum=0
while read x
do      ((Detail_Amount_Sum+=x))
done < test.txt
printf "%.2f\n" "$Detail_Amount_Sum"

which with your sample input prints:
Code:
0.00

These 3 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-15-2013
You may use paste and bc.

Code:

paste -sd+ test.txt | bc

Assuming you don't have any extra lines and all lines are numerics.
These 2 Users Gave Thanks to soliton For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

2. Shell Programming and Scripting

Mismatch in summing a column in UNIX

Hello, I am facing issue in summing up a column in unix.I am displaying a column sum up to 4 decimal places and below is the code snippet sed '1d' abc.csv | cut -d',' -f7 | awk '{s+=$1}END{ printf("%.4f\n",s)}' -170552450514.8603 example of data values in the column(not... (3 Replies)
Discussion started by: karthik adiga
3 Replies

3. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

5. Shell Programming and Scripting

summing the digits of a binary nuMBER

please help me write a perl program to find the difference of 1 and zeros of a 6 digit binary number. eg If input is 111100 expected output +2 if input is 000011 expected output -2 input is 000111 expected output 0 (2 Replies)
Discussion started by: dll_fpga
2 Replies

6. Shell Programming and Scripting

Summing up rows data regarding 1st column

Dear all, I have one file like LABEL A B C D E F G H I J K L M N G02100 64651.3 25630.7 8225.21 51238 267324 268005 234001 52410.9 18598.2 10611 10754.7 122535 267170 36631.4 G02100 12030.3 8260.15 8569.91 ... (4 Replies)
Discussion started by: AAWT
4 Replies

7. Shell Programming and Scripting

Summing column value - using PERL

I'm new to perl programming. I've a csv file as below. 20100221, abc_1, 200 20100221, abc_4, 350 20100221, opq_3, 200 20100221, abc_5, 220 20100221, xyz_1, 500 20100221, abc_2, 500 20100221, abc_3, 100 20100221, xyz_2, 700 20100221, opq_2, 350 20100221, xyz_3, 100 20100221, opq_1, 230... (8 Replies)
Discussion started by: ganapati
8 Replies

8. Shell Programming and Scripting

summing values of a column

I have a file which contains data as below: ----------------------------------------------------------------------------------------------- GSPWeb Statistics for the period of last 20 days... (3 Replies)
Discussion started by: mohsin.quazi
3 Replies

9. UNIX for Dummies Questions & Answers

summing according to the column

I have a text file with two columns the first column is an integer and the second column is date how do i sum up the first column according to the date example 123 jan1 232 jan1 473 jan2 467 jan2 356 jan3 376 jan3 my result should be 355 jan1 940 jan2 732 jan3 how do i... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

Summing on column

Hi Friends How to do sum on a column? I have a file like: FRED 500.01 TX SMITH 50.10 NY HARRY 5.00 CA 555.11 Sum on second column. I am trying using nawk like nawk 'BEGIN {FS="|"}; {printf $1"+"}' Thanks a lot for your help S :) (2 Replies)
Discussion started by: sbasetty
2 Replies
Login or Register to Ask a Question