Bash team for the account group

I have a file in the following format

abc|1
def|2
abc|8
def|3
abc|5
xyz|3

I need to group these words in the first column and summarize the value of the second column. For example, the output of this file should be

abc|14
def|5
xyz|3

Explanation: The corresponding values โ€‹โ€‹for the word "abc" are 1, 8, and 5. By adding these numbers, the sum is 14, and the output becomes "abc | 14". Similarly, for the word "def", the corresponding values โ€‹โ€‹are 2 and 3. To summarize, the final output is "def | 5".

Thank you very much for your help:)

I tried the following command

awk -F "|" '{arr[$1]+=$2} END {for (i in arr) {print i"|"arr[i]}}' filename

the other team I found was

awk -F "," 'BEGIN { FS=OFS=SUBSEP=","}{arr[$1]+=$2 }END {for (i in arr) print i,arr[i]}' filename

Both did not show me the expected results. Although I also doubt the work of these teams.

+4
source share
3

, , Awk, sort/uniq Awk, Awk.

GNU Awk gawk, PROCINFO["sorted_in"] ( specific) , Awk .

,

@ind_str_asc ; . ( , a[2*5] = 1 10, 10.)

, END,

END{PROCINFO["sorted_in"]="@ind_str_asc"; for (i in unique) print i,unique[i]}

,

awk '
    BEGIN{FS=OFS="|"}{
        unique[$1]+=$2; 
        next
    }
    END{
        PROCINFO["sorted_in"]="@ind_str_asc"; 
        for (i in unique) 
            print i,unique[i]
    }' file
+3

GNU datamash:

datamash -s -t\| -g1 sum 2 < filename

:

abc|14
def|5
xyz|3

  • -t\| -

  • -g1 - 1-

  • sum 2 -

+1
 awk -F\| '{ arry[$1]+=$2 } END { asorti(arry,arry2);for (i in arry2) { print arry2[i]"|"arry[arry2[i]]} }' filename

Your initial solution should work separately from the sorting problem. Use the asorti function to sort indexes from arry to arry2, and then loop through them.

+1
source

Source: https://habr.com/ru/post/1683803/


All Articles