-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrip_prefix.py
More file actions
executable file
·33 lines (25 loc) · 896 Bytes
/
strip_prefix.py
File metadata and controls
executable file
·33 lines (25 loc) · 896 Bytes
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
#!/usr/bin/env python
import argparse
p = argparse.ArgumentParser(description='Prints each line of a file without their commmon prefix')
p.add_argument('file')
p.add_argument('--prefix', action='store_true', help='print prefix')
p.add_argument('--prefix-length', action='store_true', help='print prefix length')
p.add_argument('--all-stripped-lines', action='store_true', help='(default) print all striped lines')
a = args = p.parse_args()
if not (a.prefix or a.prefix_length or a.all_stripped_lines):
a.all_stripped_lines = True
lines = iter(l.strip('\n') for l in open(args.file))
l = next(lines)
prefix = l
for l in lines:
i = len(prefix)
while prefix[:i] != l[:i]:
i = i - 1
prefix = prefix[:i]
if args.prefix:
print(prefix)
if args.prefix_length:
print(len(prefix))
if args.all_stripped_lines:
for l in open(args.file):
print(l[len(prefix):])