Mike Putnam

All posts ...
Connect via: Github Google+ Instagram LinkedIn Twitter Youtube
Get involved: Appleton Makerspace Wisconsin Linux Users


A simple python grep

Here's a simplistic text search script (python grep) that can be run quickly from a Windows command line or via your favorite editor (gvim) ;)

import os
import sys

if sys.argv[1] == '':
    print "Usage: python grep.py searchterm pathtosearch"
    sys.exit()
searchterm = sys.argv[1]
pathtosearch = sys.argv[2]
os.chdir(pathtosearch)
for root,dir,files in os.walk(pathtosearch):
    for fi in files:
        path = os.path.join(root,fi)
        for num,line in enumerate(open(path)):
            if searchterm.upper() in line.upper():
                print "[%s] found in [%s] , line number [%d]" %(searchterm.upper(),path,num+1)