Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import re
- class fileSort:
- isstdinempty = sys.stdin.isatty()
- def __init__(self):
- self.string_list = list()
- if not self.isstdinempty:
- self.pipe_input = sys.stdin.readlines()
- else:
- self.file_input = sys.argv[1:]
- self.read_files_or_pipe()
- def read_files_or_pipe(self):
- if not self.isstdinempty:
- self.string_list = [i.strip() for i in self.pipe_input]
- else:
- for arg in self.file_input:
- with open(arg, 'r') as stream_in:
- self.string_list += [i.strip() for i in stream_in.readlines()]
- def __enter__(self):
- return self
- def __exit__(self, _ex_type, _ex_val, _tb):
- pass
- @staticmethod
- def sort(key):
- try:
- return re.search(r'(\d+)(\D*)$', key).group(1)
- except AttributeError:
- return re.search(r'^(\W*)(\w+)', key).group(2)
- def sort_and_print(self):
- for i in sorted(self.string_list, key=self.sort):
- print(i)
- print(end='', flush=True)
- if __name__ == "__main__":
- with fileSort() as fs:
- fs.sort_and_print()
Add Comment
Please, Sign In to add comment