Skip to content

Instantly share code, notes, and snippets.

@non
Last active September 30, 2015 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non/1753093 to your computer and use it in GitHub Desktop.
Save non/1753093 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import random
import sys
if __name__ == "__main__":
args = sys.argv[1:]
if args:
need = int(args[0])
f = open(args[1], 'r') if len(args) > 1 else sys.stdin
else:
print "usage: %s NUM [FILE] - pluck NUM lines from FILE (default stdin)" % args[0]
sys.exit(0)
# generalized knuth's shuffle to choose some number of lines to display
kept = []
i = 0
for line in f:
if i < need:
kept.append(line)
else:
j = random.randint(0, i)
if j < need:
kept[j] = line
i += 1
for line in kept:
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment