annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/env python3
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 # Merge counts by key from the output of "uniq -c" (or sus) and sort in descending order
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 # An alternative to sus when the scale is too big for the initial sort, or if uniq -c already does a lot
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 # of the work
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 # Usage: ... | uniq -c | uniq-merge.py [-c]
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 # If -c, remove commas from count field
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 import sys
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 from collections import defaultdict
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 s=defaultdict(int)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 if len(sys.argv)==2 and sys.argv[1]=='-c':
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 for l in sys.stdin:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 try:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 (i,d)=l.split(maxsplit=1)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 except ValueError:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 sys.stderr.write("bogus input: %s"%l)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 continue
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 s[d]+=int(i.replace(',',''))
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 else:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 for l in sys.stdin:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 try:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 (i,d)=l.split(maxsplit=1)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 except ValueError:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23 sys.stderr.write("bogus input: %s"%l)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 continue
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 s[d]+=int(i)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 ss=sorted(s.items(),key=lambda j:j[1],reverse=True)
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 fmt='%'+str(len(str(ss[0][1]))+1)+'d\t%s'
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 for (d,n) in ss:
345f8b12fd2f mostly quite old
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 sys.stdout.write(fmt%(n,d))