title: Unix Shell: Summing up values, one per line but skipping every nth row
body: <p>I am trying to design a Unix shell script (preferably generic sh) that will take a file whose contents are numbers, one per line. These numbers are the CPU idle time from mpstat obtained by:</p>
  
  <pre><code>cat ${PARSE_FILE} | awk '{print $13}' | grep "^[!0-9]" &gt; temp.txt
  </code></pre>
  
  <p>So the file is a list if numbers, like:</p>
  
  <pre><code>46.19
  93.41
  73.60
  99.40
  95.80
  96.00
  77.10
  99.20
  52.76
  81.18
  69.38
  89.80
  97.00
  97.40
  76.18
  97.10
  </code></pre>
  
  <p>What these values really are is that line 1 is for Core 1, line 2 for Core 2, etc... for X number of cores (in my case 8) - so every 9th line is again for Core 1, etc...</p>
  
  <p>The original file looks something like this:</p>
  
  <pre><code>10/28/2013  Linux 2.6.32-358.el6.x86_64 (host)  10/28/2013  _x86_64_    
  
  (32 CPU)
  10/28/2013  
  10/28/2013  02:25:05 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
  10/28/2013  02:25:15 PM    0   51.20    0.00    2.61    0.00    0.00    0.00    0.00    0.00   46.19
  10/28/2013  02:25:15 PM    1    6.09    0.00    0.50    0.00    0.00    0.00    0.00    0.00   93.41
  10/28/2013  02:25:15 PM    2   25.20    0.00    1.20    0.00    0.00    0.00    0.00    0.00   73.60
  10/28/2013  02:25:15 PM    3    0.40    0.00    0.20    0.00    0.00    0.00    0.00    0.00   99.40
  10/28/2013  02:25:15 PM    4    3.80    0.00    0.40    0.00    0.00    0.00    0.00    0.00   95.80
  10/28/2013  02:25:15 PM    5    3.70    0.00    0.30    0.00    0.00    0.00    0.00    0.00   96.00
  10/28/2013  02:25:15 PM    6   21.70    0.00    1.20    0.00    0.00    0.00    0.00    0.00   77.10
  10/28/2013  02:25:15 PM    7    0.70    0.00    0.10    0.00    0.00    0.00    0.00    0.00   99.20
  10/28/2013  02:25:25 PM    0   45.03    0.00    1.61    0.00    0.00    0.60    0.00    0.00   52.76
  10/28/2013  02:25:25 PM    1   17.82    0.00    1.00    0.00    0.00    0.00    0.00    0.00   81.18
  10/28/2013  02:25:25 PM    2   29.62    0.00    1.00    0.00    0.00    0.00    0.00    0.00   69.38
  10/28/2013  02:25:25 PM    3    9.70    0.00    0.40    0.00    0.00    0.10    0.00    0.00   89.80
  10/28/2013  02:25:25 PM    4    2.40    0.00    0.60    0.00    0.00    0.00    0.00    0.00   97.00
  10/28/2013  02:25:25 PM    5    2.00    0.00    0.60    0.00    0.00    0.00    0.00    0.00   97.40
  10/28/2013  02:25:25 PM    6   22.92    0.00    0.90    0.00    0.00    0.00    0.00    0.00   76.18
  10/28/2013  02:25:25 PM    7    2.40    0.00    0.50    0.00    0.00    0.00    0.00    0.00   97.10
  </code></pre>
  
  <p>I'm trying to design a script that will take the number of cores and this file as a variable and get me the average for each core and I'm not sure how to do this. Here is what I have:</p>
  
  <pre><code>cat ${PARSE_FILE} | awk '{print $13}' | grep "^[!0-9]" &gt; temp.txt
  NUMBER_OF_CORES=8
  NUMBER_OF_LINES=`awk ' END { print NR } ' temp.txt`
  NUMBER_OF_VALUES=`echo "scale=0;${NUMBER_OF_LINES}/${NUMBER_OF_CORES}" | bc`
  for i in `seq 1 ${NUMBER_OF_CORES}`
  do
    awk 'NR % $i == 0' temp.txt
    echo Core: ${i} Average: xx
  done
  </code></pre>
  
  <p>So I have the number of values (lines over cores) that each core has, so that is every nth line I need to skip but I'm not sure how to cleanly do this. I basically need to loop every "NUMBER_OF_CORES" times through the file, skipping every "NUMBER_OF_CORES" line and summing them up to divide by "NUMBER_OF_VALUES".</p>