您能帮我解决以下问题吗:
我有以下输入:
pmNoNormalRabReleaseSpeech 1467 1281 1121 1046
pmNoRabEstablishAttemptSpeech 1479 1282 1128 1026
pmNoRabEstablishSuccessSpeech 1479 1280 1128 1025
pmNoNormalRabReleaseSpeech 2637 2538 1948 1833
pmNoNormalRabReleaseSpeech 2406 2423 1958 1803
pmNoRabEstablishAttemptSpeech 2717 2593 2001 1890
pmNoRabEstablishSuccessSpeech 2712 2587 2000 1885
我希望第一列中的每个唯一参数名称总结每个单独列中的值,这意味着我希望使用 awk 输出以下内容:
pmNoNormalRabReleaseSpeech 6510 6242 5027 4682
pmNoRabEstablishAttemptSpeech 4196 3875 3129 2916
答案1
假设您不关心输出行的顺序(如果您这样做,那么这是一个微不足道的调整),然后使用 GNU awk 进行多维数组:
awk '
{
for (i=2; i<=NF; i++) {
cnt[$1][i] += $i
}
}
END {
for (key in cnt) {
printf "%s", key
for (i=2; i in cnt[key]; i++) {
printf " %d", cnt[key][i]
}
print ""
}
}
' file
pmNoRabEstablishAttemptSpeech 4196 3875 3129 2916
pmNoNormalRabReleaseSpeech 6510 6242 5027 4682
pmNoRabEstablishSuccessSpeech 4191 3867 3128 2910
对于任何 awk 来说都是:
awk '
{
keys[$1]
for (i=2; i<=NF; i++) {
cnt[$1,i] += $i
}
}
END {
for (key in keys) {
printf "%s", key
for (i=2; i<=NF; i++) {
printf " %d", cnt[key,i]
}
print ""
}
}
' file
pmNoRabEstablishAttemptSpeech 4196 3875 3129 2916
pmNoNormalRabReleaseSpeech 6510 6242 5027 4682
pmNoRabEstablishSuccessSpeech 4191 3867 3128 2910