]> code.delx.au - monosys/commitdiff
Added line count script
authorJames Bunton <jamesbunton@fastmail.fm>
Wed, 8 Aug 2007 14:15:38 +0000 (00:15 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Wed, 8 Aug 2007 14:15:38 +0000 (00:15 +1000)
scripts/count.py [new file with mode: 0755]

diff --git a/scripts/count.py b/scripts/count.py
new file mode 100755 (executable)
index 0000000..63ba199
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# Print a line count. Given an input file like this:
+# apple
+# apple
+# banana
+# carrot
+# apple
+#
+# Your output will be
+# apple 2
+# banana 1
+# carrot 1
+# apple 1
+
+import sys
+
+last = ""
+count = 0
+for line in sys.stdin:
+       line = line.strip()
+       if line == last:
+               count += 1
+       else:
+               if last:
+                       print last, count
+               last = line
+               count = 1
+
+print last, count
+