1

This question already has an answer here:

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

marked as duplicate by Eric Renouf, Satō Katsura, don_crissti, HalosGhost, Anthony Geoghegan Oct 3 '16 at 13:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is the 1st field (AC1481523) always the same for the entire file or do you need different results for different 1st fields? – terdon Oct 3 '16 at 12:05
  • There are multiple entries in the first column and there are 200 columns (with numeric values) so I want to add all the columns based on the same entry in the first column. – KHAN irfan Oct 3 '16 at 12:08
  • OK, then please edit your question and clarify that. We can't guess what your input is. – terdon Oct 3 '16 at 12:09
  • I have updated my question – KHAN irfan Oct 3 '16 at 12:15
  • @KHANirfan check the link again, I believe it it does keep each column separate – Eric Renouf Oct 3 '16 at 12:42
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.