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