Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created May 2, 2014 14:18
Show Gist options
  • Save highfestiva/4a7509e645082c29a389 to your computer and use it in GitHub Desktop.
Save highfestiva/4a7509e645082c29a389 to your computer and use it in GitHub Desktop.
Sort input from pipe or files on command line
#!/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