1. Atsushi Odagiri
  2. textutils

Source

textutils / grep.py

import sys
import re
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('pattern')
parser.add_argument('filename', nargs="+")

args = parser.parse_args()

pattern = args.pattern
regex = re.compile(pattern)

for filename in args.filename:
    try:
        f = open(filename)
        for i, line in enumerate(f):
            if regex.search(line):
                print('%s:%d:' % (filename, i), line)
    except IOError as e:
        print(e)