  
<div id="snippet_15" class="snippet">
  <h2><a href="/snippets/15-Log-file-analysis-with-AWK-Calculating-the-sum-and-average" title="Log file analysis with AWK - Calculating the sum and average - Shell Script (Bash) - awk, shell, performance, average, sum">Log file analysis with AWK - Calculating the sum and average</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>This  AWK  script is useful when you want to calculate the average and sum for a set of values found in a log file.</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> awk <span class="String"><span class="String">'</span>{ s += $1 } END { print &quot;sum: &quot;, s, &quot; average: &quot;, s/NR, &quot; samples: &quot;, NR }<span class="String">'</span></span> rails_production_log_or_whatever.log
</pre></p>


	<p>Note that <strong>$1</strong> means that column one contains the values you want to use. <strong>NR</strong> is the total number of rows in the file. As an example, let&#8217;s say you have this log file:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> 1
<span class="line-numbers">   2 </span> 2
<span class="line-numbers">   3 </span> 3
<span class="line-numbers">   4 </span> 4
<span class="line-numbers">   5 </span> 5
</pre></p>


	<p>The output would then be:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> sum:  15  average:  3  samples:  5
</pre></p>


	<p>Combine it with <strong>grep</strong> or <strong>sed</strong> to do more advanced log file analysis &#8212;you can for example calculate the average time it took to render action xyz in Rails on the 21th of July at 21:00 PM.</p>


	<p>Note that by default the column values should be space separated&#8212;use the following switch to parse  CSV  (comma separated) files: <strong>-F,</strong></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/59-awk">awk</a>, <a href="/tags/60-shell">shell</a>, <a href="/tags/7-performance">performance</a>, <a href="/tags/61-average">average</a>, <a href="/tags/62-sum">sum</a>
    
    
  </div>
</div>






