summaryrefslogtreecommitdiffstats
path: root/Lib/lib-old/grep.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1990-12-26 15:40:07 (GMT)
committerGuido van Rossum <guido@python.org>1990-12-26 15:40:07 (GMT)
commit217a5fa3c33ae58c1fe420f94eeb7e806961c3c1 (patch)
treeb0bdfd34e5a72346a63106565acc7622a28b2a30 /Lib/lib-old/grep.py
parent66a07c07a5b7b1ce7150d5c5c1a0998d062e452e (diff)
downloadcpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.zip
cpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.tar.gz
cpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.tar.bz2
Initial revision
Diffstat (limited to 'Lib/lib-old/grep.py')
-rw-r--r--Lib/lib-old/grep.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/lib-old/grep.py b/Lib/lib-old/grep.py
new file mode 100644
index 0000000..210f9f0
--- /dev/null
+++ b/Lib/lib-old/grep.py
@@ -0,0 +1,32 @@
+# 'grep'
+
+import regexp
+import string
+
+def grep(expr, filename):
+ prog = regexp.compile(expr)
+ fp = open(filename, 'r')
+ lineno = 0
+ while 1:
+ line = fp.readline()
+ if not line: break
+ lineno = lineno + 1
+ res = prog.exec(line)
+ 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