Created
May 2, 2014 14:18
-
-
Save highfestiva/4a7509e645082c29a389 to your computer and use it in GitHub Desktop.
Sort input from pipe or files on command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re, sys | |
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')): | |
return [int(text) if text.isdigit() else text.lower() | |
for text in re.split(_nsre, s)] | |
fs = [sys.stdin] if not sys.stdin.isatty() else [open(fn) for fn in sys.argv[1:]] | |
for f in fs: | |
for line in sorted(f, key=natural_sort_key): | |
print(line, end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment