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.
source
share