#!/usr/bin/env python #--------------------------------------------------- # This program searches for a word in list of files. #--------------------------------------------------- import sys n = len(sys.argv[1:]) if (n < 2): print """ This script attempts to search a word in a provided list of files. Usage: ./searchWord_in_Files.py word file ./searchWord_in_Files.py word file1 file2 file3 ... ./searchWord_in_Files.py word *txt """ sys.exit(1) #----------------------------------- # Get the word from the command line #----------------------------------- word = str(sys.argv[1]) #------------------------------------------- # Start the search by looping over the files #------------------------------------------- for fName in sys.argv[2:]: # Open the file f = open(fName,'r') lineNum = 0 # Loop over each line in the file for line in f.readlines(): lineNum += 1 # Check if the word is in the line # Note that we convert everything to lower cases if word.lower() in line.lower(): print "\033[31m%5i: \033[94m%s \033[30m%s" %(lineNum, fName, line) f.close()