#!/usr/bin/env python # Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) # # This work is licensed under the Creative Commons Attribution-NonCommercial- # ShareAlike License. To view a copy of this license, visit # http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to # Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, # 94105, USA. import sys,string def chomp(line): return line.split("\n")[0] if len(sys.argv) != 3: print >>sys.stderr, \ "Usage:\n\t%s (search term) (file with list of terms)" % sys.argv[0] sys.exit(-1) term = sys.argv[1]; file_name = sys.argv[2] # read in the entries fh=open(file_name) lines=map(chomp, fh.readlines()) # rstrip not used in order to better match fh.close() # the other code samples (c, perl, ruby, etc.) # check to see if there are any matching entries if not term in lines: print >>sys.stder, "Search term not found.\n"; exit -1 # display line numbers of matching entries removed_lines = 0 while term in lines: line_number = lines.index(term)+1 print "found match on line: %d" % (line_number+removed_lines) lines = lines[line_number:] removed_lines += line_number