0

Could you please help me through the below issue:

I have below input:

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

and I want for each unique parameter name in first column sum up their values in each separate column, it means I want below output using awk:

pmNoNormalRabReleaseSpeech 6510 6242 5027 4682

pmNoRabEstablishAttemptSpeech 4196 3875 3129 2916
0

Assuming you don't care about the order of the output lines (if you do it's a trivial tweak) then using GNU awk for multi-dimensional arrays:

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

With any awk it'd be:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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