使用awk中其他列的引用来计算列的总和 [英] Calculate sum of column using reference of other column in awk
问题描述
我有一个包含2列的文件.第一列包含一些关键字,第二列包含其大小.关键字可以重复如下:
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
data3 4
data2 6
data1 3
data2 8
我想计算与相同关键字绑定的大小之和.
I want to calculate sum of sizes which are bound with same keyword.
例如,上述数据的输出为:
For example output of above data will be:
data1 8
data2 21
data3 4
可以使用awk吗?
如果是,那么请指导我.
If yes then kindly guide me.
推荐答案
您可以对数组执行awk
:
awk '{a[$1]+=$2} END {for (i in a) print i,a[i]}' file
data1 8
data2 21
data3 4
工作原理
a[$1]
使用字段#1
作为参考的名为a
的创建数组.
a[$1]+=$2
等同于a[$1]=a[$1]+$2
将字段#2
的值添加到数组a[$1]
for (i in a)
循环遍历数组a[$1]
中的所有值
print i,a[i]
打印数组i
和数组a[i]
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]
这篇关于使用awk中其他列的引用来计算列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!