Created
July 1, 2017 13:34
-
-
Save bulv1ne/963e6ab308ef23c9ad63d25cd78368d5 to your computer and use it in GitHub Desktop.
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 python | |
import argparse | |
import sys | |
def line_numbers(lnos): | |
l = [] | |
for lno in map(lambda v: v.split('-'), lnos.split(',')): | |
try: | |
if len(lno) == 1: | |
l.append((int(lno[0]), int(lno[0]))) | |
else: | |
l.append((int(lno[0]), int(lno[1]))) | |
except: | |
raise argparse.ArgumentTypeError('Could not parse %s as a line number' % lno) | |
return l | |
parser = argparse.ArgumentParser(description='Prints the lines in range') | |
parser.add_argument('linenumbers', type=line_numbers) | |
parser.add_argument('file', type=argparse.FileType('r'), help='File to read from') | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
lines = list(args.file.readlines()) | |
for lnos in args.linenumbers: | |
sys.stdout.write(''.join(lines[lnos[0] - 1:lnos[1]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment