Calculate sum of column using reference of other column in awk

前端 未结 2 1290
误落风尘
误落风尘 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:06

    If you want to keep output in same order as the input then use this little longer awk:

    awk '$1 in a{a[$1]+=$2; next} {b[++k]=$1; a[$1]=$2}
                 END{for(i=1; i<=k; i++) print b[i], a[b[i]]}' file
    data1 8
    data2 21
    data3 4
    
    0 讨论(0)
  • 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]

    0 讨论(0)
提交回复
热议问题