C. Sharp
C. Sharp

Reputation: 13

AWK (or something else) Average of multiple columns from multiple files

I would appreciate some help with an awk script, or whatever would do the job.

So, I've got multiple files (the same amount of lines and columns) and I want to do an average of every number in every column (except the first) from all the files. I have got no idea how many columns there are in a file (though i could probably get the number if needed).

filename.1

1 1 2 3 4
2 3 4 5 6
3 2 3 5 6

filename.2

1 3 4 6 6
2 5 6 7 8
3 4 5 7 8

output

1 2 3 5 5
2 4 5 6 7
3 3 4 6 7

I've found this somewhere on here that does it for a single column (as far as I understand it

awk '{a[FNR]+=$2;b[FNR]++;}END{for(i=1;i<=FNR;i++)print i,a[i]/b[i];}' fort.*

So the only? change would be to replace the +=$2 with a cycle over all columns? Is there a way to do that without knowing the exact number of columns?

Thanks.

Upvotes: 1

Views: 632

Answers (1)

Ed Morton
Ed Morton

Reputation: 204731

$ cat tst.awk
{
    key[FNR] = $1
    for (colNr=2; colNr<=NF; colNr++) {
        sum[FNR,colNr] += $colNr
    }
}
END {
    for (rowNr=1; rowNr<=FNR; rowNr++) {
        printf "%s%s", key[rowNr], OFS
        for (colNr=2; colNr<=NF; colNr++) {
            printf "%s%s", int(sum[rowNr,colNr]/ARGIND+0.5), (colNr<NF ? OFS : ORS)
        }
    }
}

$ awk -f tst.awk file1 file2
1 2 3 5 5
2 4 5 6 7
3 3 4 6 7

The above uses GNU awk for ARGIND, with other awks just add a line FNR==1{ARGIND++} at the start.

Upvotes: 4

Related Questions