1990-12-26 15:40:07 +00:00
|
|
|
# 'grep'
|
|
|
|
|
|
|
|
import regexp
|
|
|
|
import string
|
|
|
|
|
|
|
|
def grep(expr, filename):
|
1991-08-16 13:24:42 +00:00
|
|
|
match = regexp.compile(expr).match
|
1990-12-26 15:40:07 +00:00
|
|
|
fp = open(filename, 'r')
|
|
|
|
lineno = 0
|
|
|
|
while 1:
|
|
|
|
line = fp.readline()
|
|
|
|
if not line: break
|
|
|
|
lineno = lineno + 1
|
1991-08-16 13:24:42 +00:00
|
|
|
res = match(line)
|
1990-12-26 15:40:07 +00:00
|
|
|
if res:
|
|
|
|
#print res
|
|
|
|
start, end = res[0]
|
|
|
|
if line[-1:] = '\n': line = line[:-1]
|
|
|
|
prefix = string.rjust(`lineno`, 3) + ': '
|
|
|
|
print prefix + line
|
|
|
|
if 0:
|
|
|
|
line = line[:start]
|
|
|
|
if '\t' not in line:
|
|
|
|
prefix = ' ' * (len(prefix) + start)
|
|
|
|
else:
|
|
|
|
prefix = ' ' * len(prefix)
|
|
|
|
for c in line:
|
|
|
|
if c <> '\t': c = ' '
|
|
|
|
prefix = prefix + c
|
|
|
|
if start = end: prefix = prefix + '\\'
|
|
|
|
else: prefix = prefix + '^'*(end-start)
|
|
|
|
print prefix
|