Mercurial > hg > cc > cirrus_shared
comparison bin/uniq_merge.py @ 2:345f8b12fd2f
mostly quite old
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Sat, 28 Feb 2026 15:52:47 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:fef95c7a391f | 2:345f8b12fd2f |
|---|---|
| 1 #!/usr/bin/env python3 | |
| 2 # Merge counts by key from the output of "uniq -c" (or sus) and sort in descending order | |
| 3 # An alternative to sus when the scale is too big for the initial sort, or if uniq -c already does a lot | |
| 4 # of the work | |
| 5 # Usage: ... | uniq -c | uniq-merge.py [-c] | |
| 6 # If -c, remove commas from count field | |
| 7 import sys | |
| 8 from collections import defaultdict | |
| 9 s=defaultdict(int) | |
| 10 if len(sys.argv)==2 and sys.argv[1]=='-c': | |
| 11 for l in sys.stdin: | |
| 12 try: | |
| 13 (i,d)=l.split(maxsplit=1) | |
| 14 except ValueError: | |
| 15 sys.stderr.write("bogus input: %s"%l) | |
| 16 continue | |
| 17 s[d]+=int(i.replace(',','')) | |
| 18 else: | |
| 19 for l in sys.stdin: | |
| 20 try: | |
| 21 (i,d)=l.split(maxsplit=1) | |
| 22 except ValueError: | |
| 23 sys.stderr.write("bogus input: %s"%l) | |
| 24 continue | |
| 25 s[d]+=int(i) | |
| 26 ss=sorted(s.items(),key=lambda j:j[1],reverse=True) | |
| 27 fmt='%'+str(len(str(ss[0][1]))+1)+'d\t%s' | |
| 28 for (d,n) in ss: | |
| 29 sys.stdout.write(fmt%(n,d)) |
