Permalink
Browse files

a dummy implementation of coreutils' shuf command in python (didn't h…

…ave shuf on Mac)
  • Loading branch information...
1 parent d175ffb commit 59888fcc59b320bd268bab1a2b2c2ac28d20e503 Barış Metin committed Oct 22, 2010
Showing with 25 additions and 0 deletions.
  1. +25 −0 shuf
View
25 shuf
@@ -0,0 +1,25 @@
+#!/usr/bin/python
+
+import sys
+import random
+from optparse import OptionParser
+
+def shuf(fpath):
+ try:
+ lines = open(fpath, 'r').readlines()
+ random.shuffle(lines)
+ return "".join(lines)
+ except IOError:
+ return ""
+
+def main():
+ parser = OptionParser()
+ parser.add_option("-o", "--output", default=None,
+ help="write result to OUTPUT instead of standard output")
+ (options, args) = parser.parse_args()
+
+ output = open(options.output, "w") if options.output else sys.stdout
+ map(output.write, map(shuf, args))
+
+if __name__ == "__main__":
+ main()

0 comments on commit 59888fc

Please sign in to comment.