Calculate sum of column using reference of other column in awk

前端 未结 2 1289
误落风尘
误落风尘 2021-01-27 19:32

I have a file which contains 2 column. first column contains some keyword and second contains its size. Keywords can be repeated like below:

data1 5
data2 7
data         


        
2条回答
  •  失恋的感觉
    2021-01-27 20:16

    You can do awk with array:

    awk '{a[$1]+=$2} END {for (i in a) print i,a[i]}' file
    data1 8
    data2 21
    data3 4
    

    How it works a[$1] this create array named a using field #1 as reference.
    a[$1]+=$2 is the same as a[$1]=a[$1]+$2 add value of field #2 to the array a[$1]
    for (i in a) loop trough all value in array a[$1]
    print i,a[i] prints the array i and the value of array a[i]

提交回复
热议问题