|
From: banzai on 29 Aug 2006 10:41 Anybody have a simple way to sum (even average) column entries in a plain text csv file in a ksh environment e.g. example file, test.dat : 2 4 3 6 4 7 would produce 9 and 17 respectively for the sum and 9.00 and 5.67 for the average. TIA
From: Radoulov, Dimitre on 29 Aug 2006 10:55 > Anybody have a simple way to sum (even average) column entries in a plain > text csv file in a ksh environment > > e.g. > > example file, test.dat : > > 2 4 > 3 6 > 4 7 > > > would produce 9 and 17 respectively for the sum > and 9.00 and 5.67 for the average. awk '{a+=$1;b+=$2}END{printf("%.2f\t%.2f\n",a,b/NR)}' test.dat Regards Dimitre
From: Chris F.A. Johnson on 29 Aug 2006 11:07 On 2006-08-29, banzai wrote: > Anybody have a simple way to sum (even average) column entries in a plain > text csv file in a ksh environment > > e.g. > > example file, test.dat : > > 2 4 > 3 6 > 4 7 > > > would produce 9 and 17 respectively for the sum > and 9.00 and 5.67 for the average. awk '{ a1 += $1; a2 += $2 } END { print a1, a2 ## totals print a1/NR, a2/NR ## averages }' -- Chris F.A. Johnson, author <http://cfaj.freeshell.org> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence
From: banzai on 29 Aug 2006 11:15 "Radoulov, Dimitre" <dradoulov@_gmail.com> wrote in message news:44f4554d$0$75037$14726298(a)news.sunsite.dk... >> Anybody have a simple way to sum (even average) column entries in a plain >> text csv file in a ksh environment >> >> e.g. >> >> example file, test.dat : >> >> 2 4 >> 3 6 >> 4 7 >> >> >> would produce 9 and 17 respectively for the sum >> and 9.00 and 5.67 for the average. > > awk '{a+=$1;b+=$2}END{printf("%.2f\t%.2f\n",a,b/NR)}' test.dat > > > Regards > Dimitre > > > yep, thanks
From: Xicheng Jia on 29 Aug 2006 12:01 banzai wrote: > Anybody have a simple way to sum (even average) column entries in a plain > text csv file in a ksh environment > > e.g. > > example file, test.dat : > > 2 4 > 3 6 > 4 7 > > > would produce 9 and 17 respectively for the sum > and 9.00 and 5.67 for the average. > > > TIA a Perl alternative to sum/ave all columns of your csv file. perl -F, -ane ' $tot[$_] += $F[$_] for (0..$#F) }{ printf "column(%02d): total %3.2f, average %4.2f\n", ++$col, $_, $_/$. for @tot ' file.csv Xicheng
|
Next
|
Last
Pages: 1 2 Prev: How to get 2 decimal places with bc? Next: sed - precutting str2 var from str1 input |