|
Prev: xargs rm -rf in crontab
Next: Dir Files & Size
From: david_kirby_byers on 5 Jul 2006 12:21 is there a simply way to get the sum of a column im trying to get a average? so suggestions would be appreciated. below is my column from that i would like to then see what percent 4765 is of the total sum of the column. 4765 1876 1107 1088 883 670 420 335 179 177 136 129 126 120 120 110 77 66 60 51 45 22 17 17 12 11 10 8 6 6 4 3 3 3 3 1 0
From: Xicheng Jia on 5 Jul 2006 12:54 david_kirby_byers(a)yahoo.com wrote: > is there a simply way to get the sum of a column im trying to get a > average? > so suggestions would be appreciated. > > below is my column from that i would like to then see what percent 4765 > is of the total sum of the column. > a FAQ:-) awk '{x+=$1}END{print 4765/x}' file perl -pe '$\+=$_}{$\=4765/$\' file perl -lne '$x+=$_}{print 4765/$x' file Xicheng
From: david_kirby_byers on 5 Jul 2006 12:56 paste -s file | sed 's/ / + /g' | bc heres what i have came up with to add column ..... david_kirby_byers(a)yahoo.com wrote: > is there a simply way to get the sum of a column im trying to get a > average? > so suggestions would be appreciated. > > below is my column from that i would like to then see what percent 4765 > is of the total sum of the column. > > 4765 > 1876 > 1107 > 1088 > 883 > 670 > 420 > 335 > 179 > 177 > 136 > 129 > 126 > 120 > 120 > 110 > 77 > 66 > 60 > 51 > 45 > 22 > 17 > 17 > 12 > 11 > 10 > 8 > 6 > 6 > 4 > 3 > 3 > 3 > 3 > 1 > 0
From: Stephane CHAZELAS on 5 Jul 2006 13:15 2006-07-5, 09:56(-07), david_kirby_byers(a)yahoo.com: > paste -s file | sed 's/ / + /g' | bc paste -sd+ file | bc -- St?phane
From: Penguiniator on 5 Jul 2006 13:35
david_kirby_byers(a)yahoo.com wrote: > is there a simply way to get the sum of a column im trying to > get a average? If your data is in a file named data and the data is in column one: sum=0 count=0 for i in $(awk '{print $1}' data);do sum=$(( $sum + $i )) count=$(( $count + 1 )) done echo $(( $sum / $count )) Or you can use bc for more precise results: echo "scale=2; $sum / $count" | bc where scale is the number of significant digits. Also, you can pipe the data from standard input with: .... | for i in $(echo $(cat) | tr ' ' '\n' | awk ...); do ... done The previous works when there is only one column of numbers in your data file. Otherwise, you will have to isolate that column first. You can use awk or cut for that. The tr in the pipeline is necessary. Otherwise, cat removes the newlines and awk sees a single record containing all your data and prints only the first field; i.e., only the first number in the data stream. And you can combine these techniques to take input from a file specified as a parameter or from redirected standard input with simple tests such as: [ -z "$1" ] && { ...; } || { ...; } or: if [ -z "$1" ]; then ...; else ...; fi; where $1 is, or is not, an empty string corresponding to the filename containing your data. > so suggestions would be appreciated. > > below is my column from that i would like to then see what > percent 4765 is of the total sum of the column. > > 4765 > .... This seems to be a slightly different problem from the first question. And the answer would depend somewhat on what 4765 represents. Is it a fixed value? Is it the last value? Is it the first value? Is it the most recent value? Or is it simply the biggest value? Determining that will help determine how to isolate it from the other values in the column. biggest value?: biggest=$(sort -gr | head -1) first value?: first=$(head -1) last value?: last=$(tail -1) fixed value?: fixed=4765 After that, the calculation is simple: echo $(( 4765 / $sum * 100 )) substituting bc as above if desired and assuming that by "average" you mean "mean". Hope this helps. |