Skip to content

Search In One File from Keywords in Another File

I needed to see if this list of email addresses were found in a logs file. So I had one file with a list of email addresses. Another file is a list emails sent. I needed to make sure that the emails were sent. Here’s a quick Python script I put together that does this:

import re
import sys

def searchInLogFile( FILE, query ): 0, 0 )  
  for line in LogFile:
    logLine = line.replace("\n","").replace("\r","").rstrip().lstrip()
    if query, logLine, re.IGNORECASE | re.MULTILINE ):
      return True

# This file has a list (\r\n delimited) email addresses.
EmailListFile = open( "email-list-internal.txt", "r")

# This is the log file which we'll use to see if email addresses are in here.
LogFile = open( "POST20100201.log", "r" )

EmailFound = []
EmailNotFound = []
breakTime = 0
# 0 = does the whole list
EmailsToSearchFor = 0

for emailLine in EmailListFile:
  email = emailLine.replace("\n","").replace("\r","").rstrip().lstrip()

  if ( searchInLogFile( LogFile, email ) ):
    print email, "was found"
    EmailFound.append( email )

    print email, "not found"
    EmailNotFound.append( email )  

  if ( EmailsToSearchFor != 0 ):
    breakTime += 1
    if ( breakTime == EmailsToSearchFor ):

# Log results to a file.
OutputFile = open( "output.log", "w" )

divider = "\n\n======== Found ========================================"
print divider
OutputFile.write( divider )
for i in EmailFound:
  print i
  OutputFile.write( "\n" + i )

divider = "\n\n======== Not Found ===================================="  
print divider
OutputFile.write( divider )

for i in EmailNotFound:
  print i
  OutputFile.write( "\n" + i )

Pretty straightforward. The script also writes a file called “output.log” which has a list of emails that were found (marked under “found”) and not found (marked under “not found”).



Dan View All

Blog owner.

%d bloggers like this: