Mercurial > hg > cc > cirrus_shared
view bin/uniq_merge.py @ 9:f6f1deba62af default tip
sic
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Thu, 11 Jun 2026 21:16:59 +0100 |
| parents | 345f8b12fd2f |
| children |
line wrap: on
line source
#!/usr/bin/env python3 # Merge counts by key from the output of "uniq -c" (or sus) and sort in descending order # An alternative to sus when the scale is too big for the initial sort, or if uniq -c already does a lot # of the work # Usage: ... | uniq -c | uniq-merge.py [-c] # If -c, remove commas from count field import sys from collections import defaultdict s=defaultdict(int) if len(sys.argv)==2 and sys.argv[1]=='-c': for l in sys.stdin: try: (i,d)=l.split(maxsplit=1) except ValueError: sys.stderr.write("bogus input: %s"%l) continue s[d]+=int(i.replace(',','')) else: for l in sys.stdin: try: (i,d)=l.split(maxsplit=1) except ValueError: sys.stderr.write("bogus input: %s"%l) continue s[d]+=int(i) ss=sorted(s.items(),key=lambda j:j[1],reverse=True) fmt='%'+str(len(str(ss[0][1]))+1)+'d\t%s' for (d,n) in ss: sys.stdout.write(fmt%(n,d))
