-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilter_lines
executable file
·66 lines (52 loc) · 2 KB
/
filter_lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Filters lines out of STDIN. Works by taking a list of lines numbers to keep as the script's first
argument. Then walks through STDIN, keeping only line numbers that were found in the list that
was read in.
Usage:
cat unfiltered_file | ./filter_lines.py [--reverse|-r] list_of_lines
"""
import argparse
import typing
import sys
def check(last_line: int, current_line: int):
if current_line <= last_line:
print('* FATAL: current line (%d) less than or equal to the last line (%d)' % (current_line, last_line), file=sys.stderr)
sys.exit(1)
return current_line
def get_next_line(filestream, lines):
"""
Returns the next line to keep.
"""
last_line = 0
if filestream is not None:
for line in filestream:
yield check(last_line, int(line.rstrip()))
elif len(lines) > 0:
for line in lines:
yield check(last_line, line)
else:
yield ''
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--file', '-f', type=argparse.FileType('r', encoding='utf-8'), default=None,
help="File containing sorted (low to high) line numbers to keep (counting from 1)")
parser.add_argument('--lines', '-l', type=int, nargs='+', default=[],
help='List of line numbers to keep.')
parser.add_argument('--remove', '-r', action='store_true',
help='Remove specified lines instead of keeping them')
args = parser.parse_args()
lines_stream = get_next_line(args.file, args.lines)
next_line_to_keep = next(lines_stream)
for i, line in enumerate(sys.stdin, 1):
if i == next_line_to_keep:
if not args.remove:
print(line, end='', flush=True)
try:
next_line_to_keep = next(lines_stream)
except StopIteration:
next_line_to_keep = 0
elif args.remove:
print(line, end='', flush=True)
if __name__ == "__main__":
main()