awk '
NR==FNR{ #operate matrix file first
A[$1] = 1 #array of words
for(i=2;i<=NF;i++)
B[$1 OFS i] = $i #array with indexes [word field_num]
next
}
$1 in A{ #if word in array A
max = $1 OFS 2
for(i in B)
if(i ~ "^" $1 && B[max] < B[i])
max = i #find maximum in B-array
print max, B[max] #output word + field_num + value
delete B[max] #exclude value from next search
}
}
' matrix list
Se a versão do awk permitir um script de matrizes de pseudo-multidimensão pode ser simplificado
awk '
NR==FNR{
for(i=2;i<=NF;i++)
A[$1][i] = $i
next
}
$1 in A{
max = 2
for(i in A[$1])
if(A[$1][max] < A[$1][i])
max = i
print $1, max, A[$1][max]
delete A[$1][max]
}
}
' matrix list