linuxshellawkstdev

how to calculate standard deviation from different colums in shell script


I have a datafile with 10 columns as given below

ifile.txt
2  4  4  2  1  2  2  4  2  1
3  3  1  5  3  3  4  5  3  3
4  3  3  2  2  1  2  3  4  2
5  3  1  3  1  2  4  5  6  8

I want to add 11th column which will show the standard deviation of each rows along 10 columns. i.e. STDEV(2 4 4 2 1 2 2 4 2 1) and so on. I am able to do by taking tranpose, then using the following command and again taking transpose

awk '{x[NR]=$0; s+=$1} END{a=s/NR; for (i in x){ss += (x[i]-a)^2} sd = sqrt(ss/NR); print sd}'

Can anybody suggest a simpler way so that I can do it directly along each row.


Solution

  • You can do the same with one pass as well.

     awk '{for(i=1;i<=NF;i++){s+=$i;ss+=$i*$i}m=s/NF;$(NF+1)=sqrt(ss/NF-m*m);s=ss=0}1' ifile.txt