Multidimensional array:awk error

 awk -F'\t' -v OFS='\t' '

 { if($2 in arr) {

         #print "Sahi", NR,arr[$2]
         for(k=2;k<=NF;k++){
#            sum[arr[$2]][k]+=$2
         }

 }
 else {
     arr[$2]=NR
     #print "awk",NR
     for (k=3;k<=NF ; k++){
         sum[NR][k]=$k
     }
 }


 }

 #-------------------
# END{
#        for (k in arr){
#            printf k"\t"
#            for(z=3;z<=NF;z++){
#                printf sum[arr[k]][z]"\t"
#            }
#            print "\n"
#        }
 #    }
#--------------------------

 ' < header.txt

Error:
awk: line 15: syntax error at or near [

I want to sum columns if there's duplicate of row in column 2.

Input format:

ASPM    ASPM    6.667   6.482
LOC400861       LOC400861       6.669   6.647
ASPM    ASPM    0.001   0.002

Outout:

ASPM    ASPM    6.668   6.484
LOC400861       LOC400861       6.669   6.647

Summed columns for ASPM

For the time being I've excluded header from input file. In future I'd like to process file as is and tweak awk code for it. :frowning:

But for now I'd like to fix error for multi-D array.

---------- Post updated at 05:39 PM ---------- Previous update was at 05:17 PM ----------

I fixed for error but I get the line printed twice:

awk -F'\t' -v OFS='\t' '

 { if($2 in arr) {
         for(k=2;k<=NF;k++){
             sum[arr[$2],k]= $k + sum[arr[$2],k]
         }

 }
 else {
     arr[$2]=NR
     for (k=3;k<=NF;k++){
         sum[NR,k] = $k;
     }
 }
 }

 #-------------------
 END{
         for (k in arr){
             printf k"\t"
             for(z=3;z<=NF;z++){
                 printf sum[arr[k],z]"\t"
             }
             printf"\n"
         }
    }
#--------------------------

 ' < header.txt

output:

I'm not sure your field delimiter is space or tab from your post, which can be fixed easily. I assumed it is space for my try:

awk -v OFS="\t"  '{A[$2]+=$3; B[$2]+=$4}END{for( i in A) print i, i, A, B}'  < head.txt

Output:

LOC400861    LOC400861    6.669    6.647
ASPM    ASPM    6.668    6.484

Thanks for your reply.
For illustration I had put 4 columns. In file I've 366 columns hence I used multi-dimensional array.

If you don't show us the real format of your input file(s) and show us the actual output you're trying to produce from that input, it is hard for us to guess at what might work for whatever you are trying to do.

Without knowing what operating system you're using, we can't know what extended features available in some versions of awk might be available for use in your script.

What do you expect to get when you calculate the sum ASPM + ASPM ?

Is the data in all of your fields starting with field #3 through field #366 in floating point format with three digits after the decimal point? If not, what is the general format and what format should be used for the output?

Notwithstanding Don Cragun's worthwhile questions, I don't think extended features necessary for this problem. Try (output formatting excluded here):

awk -F'\t' -v OFS='\t' '
        {arr[$2] = $1
         for (k=3; k<=NF; k++)  sum[arr[$2],k] += $k
        }
END     {for (k in arr) {printf "%s%s%s%s", arr[k], OFS, k, OFS
                         for (z=3; z<=NF; z++)  printf "%s%s", sum[arr[k],z], (z==NF)?ORS:OFS
                        }
        }
' file
ASPM    ASPM    6.668    6.484
LOC400861    LOC400861    6.669    6.647

Thanks for the reply.
Do you mind explaining:

(z==NF)?ORS:OFS

man awk :

The logical expression in front of the ? is evaluated; if TRUE, the value of the expression between that and the : is the result to be used / assigned, if FALSE, the trailing one. Available in many other languages and shells as well.

Assuming your col-1 and col-2 are the same, this worked for me with your example data with hard-coded tab and format %4.4f, which need be changed according to your real data:

awk -v OFS="\t" '{for (i=3; i<=NF; i++) A[$2]+=$i} END{for(k in A) { printf("%s\t%s\t", k, k); for (j=3; j<=NF; j++) {printf("%4.4f\t",  A[k][j])}; printf("\n")}}' infile.dat