diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/uniq_merge.py	Sat Feb 28 15:52:47 2026 +0000
@@ -0,0 +1,29 @@
+#!/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))