Skip to content

Instantly share code, notes, and snippets.

@chrisma
Last active January 28, 2022 19:34
Embed
What would you like to do?
sum and count columns from stdin, because I'm no good at awk
import sys
d = {}
for line in sys.stdin:
adds, dels, path = line.rstrip().split('\t')
if path in d:
d[path]['adds'] += int(adds)
d[path]['dels'] += int(dels)
d[path]['count'] += 1
else:
d[path] = {'adds': int(adds), 'dels': int(dels), 'count': 1}
for path, vals in d.items():
sys.stdout.write(f"{vals['count']}\t{vals['adds']}\t{vals['dels']}\t{path}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment