GROUP BY / SUM from shell [英] GROUP BY/SUM from shell

查看:177
本文介绍了GROUP BY / SUM from shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含如下数据的大文件:

I have a large file containing data like this:

a 23
b 8
a 22
b 1

我想要能够得到这个:

a 45
b 9

我可以先排序这个文件,然后在Python中通过扫描文件一次。什么是良好的直接命令行方式?

I can first sort this file and then do it in Python by scanning the file once. What is a good direct command-line way of doing this?

推荐答案

awk '{
    arr[$1]+=$2
   }
   END {
     for (key in arr) printf("%s\t%s\n", key, arr[key])
   }' file \
   | sort +0n -1

我希望这有助。

这篇关于GROUP BY / SUM from shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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