Sponsored Content
Top Forums Shell Programming and Scripting How to add lines of a file and average them Post 302604736 by agama on Monday 5th of March 2012 10:52:55 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
NL(1)                                                              User Commands                                                             NL(1)

NAME
nl - number lines of files SYNOPSIS
nl [OPTION]... [FILE]... DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -b, --body-numbering=STYLE use STYLE for numbering body lines -d, --section-delimiter=CC use CC for logical page delimiters -f, --footer-numbering=STYLE use STYLE for numbering footer lines -h, --header-numbering=STYLE use STYLE for numbering header lines -i, --line-increment=NUMBER line number increment at each line -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one -n, --number-format=FORMAT insert line numbers according to FORMAT -p, --no-renumber do not reset line numbers for each section -s, --number-separator=STRING add STRING after (possible) line number -v, --starting-line-number=NUMBER first line number for each section -w, --number-width=NUMBER use NUMBER columns for line numbers --help display this help and exit --version output version information and exit By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are two delimiter characters used to construct logical page delimiters, a missing second character implies :. Type \ for . STYLE is one of: a number all lines t number only nonempty lines n number no lines pBRE number only lines that contain a match for the basic regular expression, BRE FORMAT is one of: ln left justified, no leading zeros rn right justified, no leading zeros rz right justified, leading zeros AUTHOR
Written by Scott Bartram and David MacKenzie. REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report nl translation bugs to <http://translationproject.org/team/> COPYRIGHT
Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
Full documentation at: <http://www.gnu.org/software/coreutils/nl> or available locally via: info '(coreutils) nl invocation' GNU coreutils 8.28 January 2018 NL(1)
All times are GMT -4. The time now is 05:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy