import re
import sys
#initialize with some default pattern
mainpattern = re.compile('.*')
def matchLine(line):
result=mainpattern.search(line)
if result is not None:
if result.group() is not None:
print result.group()
def matchLines(inputfile):
for line in inputfile:
matchLine(line)
def processFile(inputfilename):
inputfile=open(inputfilename,'r')
matchLines(inputfile)
def main():
processFile(sys.argv[1])
if __name__=="__main__":
if sys.argv.__len__()<3:
print 'usage: match [infile] [pattern]'
else:
mainpattern=re.compile(sys.argv[2])
main()
Tuesday, 10 March 2009
Python Script: Extract strings matching a regex from a file
A simple python script to extract strings matching a regex from a file.
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment