title: How do I calculate the standard deviation in my shell script?
body: <p>I have a shell script: </p>
  
  <pre><code>dir=$1 
  cd $dir 
  grep -P -o '(?&lt;=&lt;rating&gt;).*' * | 
  awk -F: '{A[$1]+=$2;L[$1]++;next}END
  {for(i in A){print i, A[i]/L[i]}}' | sort -nr -k2 | 
  awk '{ sub(/.dat/, " "); print }'
  </code></pre>
  
  <p>which sums up all of the numbers that follow the <code>&lt;rating&gt;</code> field in each file of my folder but now I need to calculate the standard deviation of the numbers rather than getting the average. By summing up the difference of each rating in the file from the mean squared and then dividing this by the sample size -1. I do not need to do this in every file in the folder, but instead in 2 specific files, <code>hotel_188937.dat</code> and <code>hotel_203921.dat</code>. Here is an example of the contents of one of these files:</p>
  
  <pre><code>&lt;Overall Rating&gt;
  &lt;Avg. Price&gt;$155
  &lt;URL&gt;
  
  &lt;Author&gt;Jeter5
  &lt;Content&gt;I hope we're not disappointed! We enjoyed New Orleans...
  &lt;Date&gt;Dec 19, 2008
  &lt;No. Reader&gt;-1
  &lt;No. Helpful&gt;-1
  &lt;rating&gt;4
  &lt;Value&gt;-1
  &lt;Rooms&gt;3
  &lt;Location&gt;5
  &lt;Cleanliness&gt;3
  &lt;Check in / front desk&gt;5
  &lt;Service&gt;5
  &lt;Business service&gt;5
  
  &lt;Author&gt;...
  repeat fields again...
  </code></pre>
  
  <p>The sample size of the first file is 127 with a mean of 4.78 compared with a sample size of 324 and a mean of 4.78 for the second file. Is there anyway that I can alter my script to calculate the standard deviation for these two specific files rather than calculating the average for every file in my directory? Thanks for your time.</p>