How to add lines of a file and average them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add lines of a file and average them
# 1  
Old 03-05-2012
How to add lines of a file and average them

I'm reading in numbers from a file and trying to add them together. Here is the code so far. I know the 1+2+3.... part is wrong. The file has five numbers in it with each number on its own line. The numbers are decimals if that matters. Thanks.

HTML Code:
while read EachLine
do
  echo $EachLine
done < myfilename
  echo sum=$(( "1 + 2 + 3 + 4 + 5" ))
fi

Last edited by AxlVanDamme; 03-05-2012 at 11:42 PM..
# 2  
Old 03-05-2012
If by "the numbers are decimals" you mean they are real numbers (1.234) and not integers (1), and you are using ksh, then you can do it right in the shell:

Code:
#!/usr/bin/env ksh
# this won't work in bash
sum=0.0
n=0
while read value
do
   sum=$(( sum + value ))
   n=$(( n + 1 ))
done <input-file-name

echo "$n values summed to $sum with an average of $(( sum/$n.0 ))"

---------- Post updated at 22:52 ---------- Previous update was at 22:48 ----------

If ksh isn't an option, you could use awk:

Code:
awk '{ sum += $1; } END {printf( "%d values summed to %.2f with an average of %.4f\n", NR, sum, sum/NR ); }'  input-file


Last edited by agama; 03-05-2012 at 11:53 PM.. Reason: comment
# 3  
Old 03-06-2012
Thanks for the reply. I'd like to keep them in a file and not in the shell though. Is there a way to do that?
# 4  
Old 03-06-2012
Quote:
Originally Posted by AxlVanDamme
Thanks for the reply. I'd like to keep them in a file and not in the shell though. Is there a way to do that?
They'd be in a file. The script, or the awk, read's the file, sums the numbers and presents the average. The 'input-file' or 'input-file-name' in the samples would be the name of your file that contains the numbers.
This User Gave Thanks to agama For This Post:
# 5  
Old 03-06-2012
You're right. Sorry, I had a brain cramp. I've tried both ways since I'm using ksh. I lose the decimal places using the ksh method, but not with the awk.
# 6  
Old 03-06-2012
ksh88 does not have floating point arithmetic, ksh93 does..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-06-2012
I got it worked out using bc. Thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Get the average of lines with the same first 4 letters

How to sum up and print into the next line the total SUM. ]$ cat hhhh aaa1a 1 aaa1g 2 aaa1f 3 baa4f 3 baa4d 4 baa4s 4 cddg1 3 cddg3 4 cddfg 1 $ cat hhhh|awk ' {sum+=$2} END {print sum}' 25 Desire output: aaa1a 1 (13 Replies)
Discussion started by: kenshinhimura
13 Replies

3. Shell Programming and Scripting

Combine identical lines and average the one variable field

I have the following file 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:1.45 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:0.283 311027 chrX_310892_311162 300 91.6452... (2 Replies)
Discussion started by: jfern
2 Replies

4. Shell Programming and Scripting

Add 3 new lines for every line in a file

I have a file that I need to add 3 new lines of text for every line in the file - when I attempt the sed scripting below I keep getting 'command garbled' errors. Any suggestions would be greatly appreciated #!/bin/ksh list=`cat /export/home/list` for i in $list do sed ' a \... (7 Replies)
Discussion started by: bjdamon
7 Replies

5. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

6. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

7. Shell Programming and Scripting

print running field average for a set of lines

Hi everyone, I have a program that generates logs that contains sections like this: IMAGE INPUT 81 0 0.995 2449470 0 1726 368 1 0.0635 0.3291 82 0 1.001 2448013 0 1666 365 1 0.0649 0.3235 83 0 1.009 2444822 0 1697 371 1 ... (3 Replies)
Discussion started by: euval
3 Replies

8. Shell Programming and Scripting

add few lines to a file in unix

Hi, I have an xml file <Disp x=y a=b Disp/> <Disp q=1 w=2 Disp/> ..... I want to add a new set in between like this for a set of 100 files. <Disp x=y a=b Disp/> <Disp k=1 z=2 Disp/> <Disp q=1 w=2 (7 Replies)
Discussion started by: naga_5star
7 Replies

9. Shell Programming and Scripting

add lines in file with perl

How to search string like: a and replace to a a a : : a in a file with perl? Thanks, Grace (6 Replies)
Discussion started by: jinsh
6 Replies

10. Shell Programming and Scripting

Add lines into file

Hi, i have a file like: 00:00 8 00:01 2 00:04 5 00:07 10 . . . and i need to add the other minutes with value 0 and have a file like: 00:00 8 00:01 2 00:02 0 00:03 0 00:04 5 00:05 0 00:06 ... (13 Replies)
Discussion started by: Lestat
13 Replies
Login or Register to Ask a Question