Skip to content

Instantly share code, notes, and snippets.

@chrisma
Last active January 28, 2022 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisma/84a9f08ccd9e20ece86be9af7c3d0019 to your computer and use it in GitHub Desktop.
Save chrisma/84a9f08ccd9e20ece86be9af7c3d0019 to your computer and use it in GitHub Desktop.
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