awk 'BEGIN{ getline; for(i=1;i<=NF;i++) hdr[i]=$i; max=-1 }
{ for(i=2; i<=NF; i+=2)
if($i==max) { lc=lc","$(i+1); sn=sn","hdr[i] }
else if($i>max) { max=$i; lc=$(i+1); sn=hdr[i] }
print $1" "lc" "sn; max=-1 }'
Aqui está com comentários
awk 'BEGIN{
# read line 1 column-headers to an array
getline; for(i=1;i<=NF;i++) hdr[i]=$i
# set max to less than zero
max=-1
}
{ # processes lines 2 -> last
# check each "sn.." field
for(i=2; i<=NF; i+=2)
if($i==max) { # this cannot occurr for 1st itteration per line
# append "lc" field value and "sn" header
# to previous "lc" and "sn", using ","
lc=lc","$(i+1); sn=sn","hdr[i]
}
else if($i>max) { # this must occurr on 1st itteration per line
# save "lc" field value and "sn" header
max=$i; lc=$(i+1); sn=hdr[i]
}
# print output for the current line
print $1" "lc" "sn;
# set max to less than zero; ready for next line
# max=-1
}
Saída
RM1 ss1 sno1
RM2 gs2 sno3
RM3 ms3 sno2
RM4 ss4,ms4 sno1,sno2