
Forums  Search Forums  Register  Forum Rules  Man Pages  Albums  FAQ  Members  Calendar  Search  Today's Posts  Mark Forums Read 
Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. 
Tags 
mean std awk 

Thread Tools  Search this Thread  Display Modes 
#1




Output mean and standard deviation of a row
I have a file that looks that this: Code:
820 890 530 1650 1600 1800 1850 1900 2270 1640 2300 1670 2080 2200 2350 1150 1630 2210 I would like to output the mean and standard deviation of each row so that my final output would look like this Code:
820 890 530 746.667 155.849 1650 1600 1800 1683.33 84.9837 1850 1900 2270 2006.67 187.32 1640 2300 1670 1870 304.302 2080 2200 2350 2210 110.454 1150 1630 2210 1663.33 433.385 mean is calculated as average and standard dev is: Code:
sqrt((sum((xmean)**2))/N) In my case N=3. Can someone help with awk Last edited by kayak; 03262013 at 11:39 AM.. 
Sponsored Links  

#2




Calculating mean is easier Code:
awk '{sum=0; for (i=1; i<=NF; i++) {sum=sum+$i;} m=sum/NF; print $0, m; }' file 
Sponsored Links  

#3




Code:
awk ' BEGIN { N = 3 } { for ( i = 1; i <= NF; i++ ) { rec[NR] = $0 sum[NR] += $i sumsq[NR] += $i * $i } } END { for ( i = 1; i <= NR; i++ ) print rec[i]" "sum[i]/N" "sqrt(sumsq[i]/N  (sum[i]/N)**2) } ' file 
#4




Why can't we use NF instead of N here??

Sponsored Links  

#5




Yes, we can use NF or 3. Does that really matter?
I defined & used variable N because the requester specified it in his formula. 
Sponsored Links  

#6




I just wanted to use NF so that we can use the same logic to many columns
And, your awk codes work perfect Started learning from you 
Sponsored Links  

Thread Tools  Search this Thread 
Display Modes  
More UNIX and Linux Forum Topics You Might Find Helpful  
Thread  Thread Starter  Forum  Replies  Last Post 
calculating rowwise standard deviation using awk  ida1215  Shell Programming and Scripting  2  07032012 03:05 PM 
AWK script for standard deviation / root mean square deviation  chrisjorg  Shell Programming and Scripting  12  01182012 12:30 PM 
Standard deviation in awk  gd9629  Shell Programming and Scripting  11  08242011 01:40 PM 
Calculating the Standard Deviation for a column  kylle345  UNIX for Dummies Questions & Answers  1  05182009 05:58 PM 
Mean and Standard deviation  lakshmikanth.pg  Shell Programming and Scripting  4  04272009 04:04 PM 
