how to get the sum of all the lines in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the sum of all the lines in the file
# 1  
Old 07-22-2008
how to get the sum of all the lines in the file

Hi

I have the following file, how I will calculate the sum of all the entries in the file.

> cat abc
2
3
4

now the sum should be 2+3+4 = 9

Last edited by sdosanjh; 07-22-2008 at 07:31 AM..
# 2  
Old 07-22-2008
with bash script :

Code:
#!/bin/bash

sum=0
count=0

for i in `cat logfile`; do sum="$sum+$i"; count=`expr $count + 1`; done

sum=$(echo $sum | bc)
echo "Sum=$sum"

The above assumes that the file you want to process is named "logfile" and you have "bc" utility installed. There's a way in perl, too :

Code:
open (DATA, $logFile) or die "Error\n";

        my @rawData=<DATA>;
        my ($sum);
        foreach $n (@rawData) {
                
                $sum += $n;
print "Sum : $sum\n";

assumes that $logFile is declared.
There should be a way with awk, but I can't think of it right now.
# 3  
Old 07-22-2008
awk 'sum=sum+$1 END{print sum}' abc
# 4  
Old 07-22-2008
Try awk
Code:
awk '{a+=$0}END{print a}' abc

# 5  
Old 07-22-2008
dear all...thanx for your quick responses..... it helped me a lot to solve the issue....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

Sum product of even/odd lines

Hi, I have a text file like this 6.0000E-02 0.00000E+00 0.0000 0.00000E+00 0.0000 7.0000E-02 5.00000E-10 1.0000 5.00000E-10 1.0000 8.0000E-02 3.00000E-09 0.4082 3.00000E-09 0.4082 9.0000E-02 3.50000E-09 0.3780 3.50000E-09 0.3780 1.0000E-01 1.00000E-09... (2 Replies)
Discussion started by: f_o_555
2 Replies

4. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies

5. Shell Programming and Scripting

Sum from successive lines following date header to create data for graphing connections

Hi, I have a log file created from a load balancer showing connections to each member of a two member pool with the following format (where first field is source IP, second field is load balanced IP address and third field is destination member. I need to plot a graph by date/time and number of... (5 Replies)
Discussion started by: shog63
5 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

7. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

8. Shell Programming and Scripting

sum of a column and selecting lines with value above threshold

Hi again, I need to further process the results of a previous manipulation. I have a file with three columns e.g. AAA5 0.00175 1.97996e-06 AAA5 0.01334 2.14159e-05 AAA5 0.01340 4.12155e-05 AAA5 0.01496 1.10312e-05 AAA5 0.51401 0.0175308 BB0 0.00204 2.8825e-07 BB0 0.01569 7.94746e-07 BB0... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. Shell Programming and Scripting

Sum value from selected lines script (awk,perl)

Hello. I face this (2 side) problem. Some lines with this structure. ........... 12345678 4 12345989 13 12346356 205 12346644 74 12346819 22 ......... The first field (timestamp) is growing (or at least equal). 1)Sum the second fields if the first_field/500 are... (8 Replies)
Discussion started by: paolfili
8 Replies

10. Shell Programming and Scripting

Sum of all lines in file without roundup with awk

Hi, I have a file and I want to sum all the numbers in it. Example of the file: 0.6714359 -3842.59553830551 I used your forum (https://www.unix.com/shell-programming-scripting/74293-how-get-sum-all-lines-file.html) and found a script, what worked for me: awk '{a+=$0}END{print a}'... (8 Replies)
Discussion started by: mario8eren
8 Replies
Login or Register to Ask a Question