合并第1列的结果,然后对第2列求和以列出第1列中每个条目的总计 [英] Combine results of column one Then sum column 2 to list total for each entry in column one

查看:178
本文介绍了合并第1列的结果,然后对第2列求和以列出第1列中每个条目的总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Bash新手,所以请在这里忍受.

I am bit of Bash newbie, so please bear with me here.

我有一个文本文件,该文件是由另一个软件(我无法控制)转储的,列出了每个用户的访问次数,其访问如下所示的某些资源的次数:

I have a text file dumped by another software (that I have no control over) listing each user with number of times accessing certain resource that looks like this:


Jim 109
Bob 94
John 92
Sean 91
Mark 85
Richard 84
Jim  79
Bob  70
John 67
Sean 62
Mark 59
Richard 58
Jim  57
Bob  55
John 49
Sean 48
Mark 46
.
.
.

我的目标是获得这样的输出.

My goal here is to get an output like this.


Jim  [Total for Jim]
Bob  [Total for Bob]
John [Total for John]

以此类推.

每次在软件中运行查询时,名称都会更改,因此对每个名称进行静态搜索,然后通过wc进行管道传输无济于事.

Names change each time I run the query in the software, so static search on each name and then piping through wc does not help.

推荐答案

这听起来像是 :)将程序的输出传递到以下awk脚本:

This sounds like a job for awk :) Pipe the output of your program to the following awk script:

your_program | awk '{a[$1]+=$2}END{for(name in a)print name " " a[name]}'

输出:

Sean 201
Bob 219
Jim 245
Mark 190
Richard 142
John 208

可以使用以下格式更好地解释awk脚本本身:

The awk script itself can be explained better in this format:

# executed on each line
{
  # 'a' is an array. It will be initialized 
  # as an empty array by awk on it's first usage
  # '$1' contains the first column - the name
  # '$2' contains the second column - the amount
  #
  #  on every line the total score of 'name' 
  #  will be incremented  by 'amount'
  a[$1]+=$2
}
# executed at the end of input
END{
  # print every name and its score
  for(name in a)print name " " a[name]
}

注意,要获得按分数排序的输出,可以将另一个管道添加到sort -r -k2. -r -k2按相反的顺序对第二列进行排序:

Note, to get the output sorted by score, you can add another pipe to sort -r -k2. -r -k2 sorts the by the second column in reverse order:

your_program | awk '{a[$1]+=$2}END{for(n in a)print n" "a[n]}' | sort -r -k2

输出:

Jim 245
Bob 219
John 208
Sean 201
Mark 190
Richard 142

这篇关于合并第1列的结果,然后对第2列求和以列出第1列中每个条目的总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆