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
1
  • Please see stackoverflow.com/help/formatting and then edit your question to make the sample input/output legible/testable and provide what you have tried so far. Commented Feb 10, 2020 at 23:22

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.