Given a file with two columns:Id  ht510 69510 67510 65510 62510 59601 29601 26601 21601 20I need a way to coalesce all rows with the same ID into one that has an average height. In this case, (69 + 67 + 65 + 62 + 59) / 5 = 64 and (29 + 26 + 21 + 20) / 4 = 24, so the output should be:Id  Avg.ht 510 64 601 24How can I do that using sed/awk/perl?
Average rows with same first column
text processing;awk;perl
Using awk :The input file$ cat FILEId  ht510 69510 67510 65510 62510 59601 29601 26601 21601 20Awk in a shell :$ awk '    NR>1{        arr[$1]   += $2        count[$1] += 1    }    END{        for (a in arr) {            print id avg  a  =  arr[a] / count[a]        }    }' FILEOr with Perl in a shell :$ perl -lane '    END {        foreach my $key (keys(%hash)) {            print id avg $key =  . $hash{$key} / $count{$key};        }    }    if ($. > 1) {        $hash{$F[0]}  += $F[1];        $count{$F[0]} += 1;    }' FILEOutput is :id avg 601 = 24id avg 510 = 64.4And last for the joke, a Perl dark-obfuscated one-liner =)perl -lane'END{for(keys(%h)){print$_:.$h{$_}/$c{$_}}}($.>1)&&do{$h{$F[0]}+=$F[1];$c{$F[0]}++}' FILE