-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygrep
executable file
·72 lines (60 loc) · 1.75 KB
/
pygrep
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
67
68
69
70
71
72
#!/usr/bin/python
"""
Yet Another grep.
But user friendly.
sudo ln -s ./pygrep /usr/local/bin
"""
import re
import click
import sys
@click.group()
def main():
pass
@main.command()
@click.argument('match_pattern')
@click.argument('replacement_pattern')
@click.argument('input', type=click.File('r'), default='-')
def replace_match(match_pattern, replacement_pattern, input):
"""
Replaces the first matching part of each line with the replacement pattern.
"""
r = re.compile(match_pattern)
for line in input:
print r.sub(replacement_pattern, line)
@main.command()
@click.argument('match_pattern')
@click.argument('input', type=click.File('r'), default='-')
@click.option('-g', '--group', default=-1)
def print_match(match_pattern, group, input):
"""
Prints the first part of each line matching a pattern.
group option:
- < 0: prints the whole match if no capturing group specified, otherwise
prints only the first capturing group.
- = 0: prints the whole match
- > 0: prints only the specified capturing group
"""
r = re.compile(match_pattern)
for line in input:
match = r.search(line)
if match:
if group >= 0:
print match.group(group)
elif match.groups():
print match.group(1)
else:
print match.group(0)
@main.command()
@click.argument('match_pattern')
@click.argument('input', type=click.File('r'), default='-')
def print_line(match_pattern, input):
"""
Prints the lines matching the given pattern.
"""
r = re.compile(match_pattern)
for line in input:
match = r.search(line)
if match:
print line
if __name__ == '__main__':
main()