forked from mabauer/7languages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.py
executable file
·41 lines (36 loc) · 1.12 KB
/
grep.py
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
#!/usr/bin/env python
# A simple grep
# Use python 3 compatible syntax or print
from __future__ import print_function
import sys
import os
import re
# Search for 'expr' in 'file', if no file is provided, stdin is used instead
def grep(expr, file=''):
try:
if not file or file == '':
file = 'stdin'
f = sys.stdin
else:
f = open(file, "rU")
for line in f:
line = line.rstrip('\n')
if re.search(expr, line):
print('{}: {}'.format(file, line))
f.close()
except IOError:
print('Error opening file \'{}\'.'.format(file), file=sys.stderr)
return
# Very simple command line parsing: sys.argv[0] == script name, sys.argv[1:] == arguments
if len(sys.argv) < 2:
# No arguments: print usage message
script = os.path.basename(sys.argv[0])
print('Usage: {} expr file...'.format(script), file=sys.stderr)
else:
expr = sys.argv[1]
print('search expression: {}'.format(expr))
if len(sys.argv) == 2:
grep(expr)
else:
for file in sys.argv[2:]:
grep(expr, file)