1

I have this output filename.txt

AC1481523 001 001 001 001
AC1481523 005 005 005 005
AC1481676 003 003 005 004
AC1481676 003 002 001 004

I want to add all the columns separately where the first column has the same value. I tried this

awk '{for (j = 1; j <= 200; j++) a[$1]+=$j} END {for(i in a) print i,a[i] }' filename.txt

I get all the numbers added in a single column, and I get

AC1481523 24
AC1481676 25

But I want

AC1481523 6 6 6 6 
AC1481676 6 5 6 8
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
KHAN irfan
  • 113
  • 1
  • 5

1 Answers1

0

Here's one way:

$ awk '{ for (j = 2; j <= NF; j++) a[$1][j]+=$j }
       END {
            for(i in a){
                printf "%s", i; 
                for(field in a[i]){ 
                    printf " %s",a[i][field] 
                } 
                print ""
            }
        }' file 
AC1481676 6 5 6 8
AC1481523 6 6 6 6

Note that I have started j counting from 2 since we don't want the 1st field and until NF (the number of fields) instead of 200. That way it will work for an arbitrary number of fields as long as it's >= 2. Then, the script is using a multidimensional array (a[$1][j]) so that for each first field, there is an array of all the associated values. Finally, we iterate over the array, printing as needed.

terdon
  • 234,489
  • 66
  • 447
  • 667