Guido van Rossum | ee60eb1 | 1998-08-12 17:47:52 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Reverse grep. |
| 4 | |
| 5 | Usage: rgrep [-i] pattern file |
| 6 | """ |
| 7 | |
| 8 | import sys |
| 9 | import re |
| 10 | import string |
| 11 | import getopt |
| 12 | |
| 13 | def main(): |
| 14 | bufsize = 64*1024 |
| 15 | reflags = 0 |
| 16 | opts, args = getopt.getopt(sys.argv[1:], "i") |
| 17 | for o, a in opts: |
| 18 | if o == '-i': |
| 19 | reflags = reflags | re.IGNORECASE |
| 20 | if len(args) < 2: |
| 21 | usage("not enough arguments") |
| 22 | if len(args) > 2: |
| 23 | usage("exactly one file argument required") |
| 24 | pattern, filename = args |
| 25 | try: |
| 26 | prog = re.compile(pattern, reflags) |
| 27 | except re.error, msg: |
| 28 | usage("error in regular expression: %s" % str(msg)) |
| 29 | try: |
| 30 | f = open(filename) |
| 31 | except IOError, msg: |
| 32 | usage("can't open %s: %s" % (repr(filename), str(msg)), 1) |
| 33 | f.seek(0, 2) |
| 34 | pos = f.tell() |
| 35 | leftover = None |
| 36 | while pos > 0: |
| 37 | size = min(pos, bufsize) |
| 38 | pos = pos - size |
| 39 | f.seek(pos) |
| 40 | buffer = f.read(size) |
| 41 | lines = string.split(buffer, "\n") |
| 42 | del buffer |
| 43 | if leftover is None: |
| 44 | if not lines[-1]: |
| 45 | del lines[-1] |
| 46 | else: |
| 47 | lines[-1] = lines[-1] + leftover |
| 48 | if pos > 0: |
| 49 | leftover = lines[0] |
| 50 | del lines[0] |
| 51 | else: |
| 52 | leftover = None |
| 53 | lines.reverse() |
| 54 | for line in lines: |
| 55 | if prog.search(line): |
| 56 | print line |
| 57 | |
| 58 | def usage(msg, code=2): |
| 59 | sys.stdout = sys.stderr |
| 60 | print msg |
| 61 | print __doc__ |
| 62 | sys.exit(code) |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | main() |