title: Calculate average of each column in a file
body: <p>I have a text file with n number of rows (separated by commas) and columns and I want to find average of each column, excluding empty field.</p>
  
  <p>A sample input looks like:</p>
  
  <pre><code>1,2,3
  4,,6
  ,7,
  </code></pre>
  
  <p>The desired output is:</p>
  
  <pre><code>2.5, 4.5, 4.5
  </code></pre>
  
  <p>I tried with </p>
  
  <pre><code>awk -F',' '{ for(i=1;i&lt;=NF;i++) sum[i]=sum[i]+$i;if(max &lt; NF)max=NF;};END { for(j=1;j&lt;=max;j++) printf "%d\t",sum[j]/max;}' input
  </code></pre>
  
  <p>But it treats consecutive delimiters as one and mixing columns. 
  Any help is much appreciated.</p>