Search code examples
awkaveragedelimiter

Calculate average of each column in a file


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.

A sample input looks like:

1,2,3
4,,6
,7,

The desired output is:

2.5, 4.5, 4.5

I tried with

awk -F',' '{ for(i=1;i<=NF;i++) sum[i]=sum[i]+$i;if(max < NF)max=NF;};END { for(j=1;j<=max;j++) printf "%d\t",sum[j]/max;}' input

But it treats consecutive delimiters as one and mixing columns. Any help is much appreciated.


Solution

  • You can use this one-liner:

    $ awk -F, '{for(i=1; i<=NF; i++) {a[i]+=$i; if($i!="") b[i]++}}; END {for(i=1; i<=NF; i++) printf "%s%s", a[i]/b[i], (i==NF?ORS:OFS)}' foo
    2.5 4.5 4.5
    

    Otherwise, you can save this in a file script.awk and run awk -f script.awk your_file:

    {
        for(i=1; i<=NF; i++) {
            a[i]+=$i
            if($i!="") 
                b[i]++}
        } 
    END {
        for(i=1; i<=NF; i++) 
            printf "%s%s", a[i]/b[i], (i==NF?ORS:OFS)
    }