Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

This is an extending question of the post Average rows with same first columnAverage rows with same first column

Input file:

a   12  13  14
b   15  16  17
a   21  22  23
b   24  25  26

Desired output:

a   16.5  17.5  18.5
b   19.5  20.5  21.5

The awk code in that post is:

awk '
    NR>1{
        arr[$1]   += $2
        count[$1] += 1
    }
    END{
        for (a in arr) {
            print a "\t" arr[a] / count[a]
        }
    }
'

Question: This code only works on the first row. How do I expand this code to multiple columns?

This is an extending question of the post Average rows with same first column

Input file:

a   12  13  14
b   15  16  17
a   21  22  23
b   24  25  26

Desired output:

a   16.5  17.5  18.5
b   19.5  20.5  21.5

The awk code in that post is:

awk '
    NR>1{
        arr[$1]   += $2
        count[$1] += 1
    }
    END{
        for (a in arr) {
            print a "\t" arr[a] / count[a]
        }
    }
'

Question: This code only works on the first row. How do I expand this code to multiple columns?

This is an extending question of the post Average rows with same first column

Input file:

a   12  13  14
b   15  16  17
a   21  22  23
b   24  25  26

Desired output:

a   16.5  17.5  18.5
b   19.5  20.5  21.5

The awk code in that post is:

awk '
    NR>1{
        arr[$1]   += $2
        count[$1] += 1
    }
    END{
        for (a in arr) {
            print a "\t" arr[a] / count[a]
        }
    }
'

Question: This code only works on the first row. How do I expand this code to multiple columns?

Source Link
Gray
  • 23
  • 3

Average all rows of multiple columns with the same first column

This is an extending question of the post Average rows with same first column

Input file:

a   12  13  14
b   15  16  17
a   21  22  23
b   24  25  26

Desired output:

a   16.5  17.5  18.5
b   19.5  20.5  21.5

The awk code in that post is:

awk '
    NR>1{
        arr[$1]   += $2
        count[$1] += 1
    }
    END{
        for (a in arr) {
            print a "\t" arr[a] / count[a]
        }
    }
'

Question: This code only works on the first row. How do I expand this code to multiple columns?