Calculate average of each column in a file

旧城冷巷雨未停 提交于 2020-01-13 11:30:10

问题


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.


回答1:


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)
}


来源:https://stackoverflow.com/questions/39121651/calculate-average-of-each-column-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!