File Parsing - How to identify specific lines only if next line contains a certain word

80 views Asked by At

I have a largish file of around 50k lines. I need to enter data from a line into a db only if the next line has a certain word in it. For instance

00:00:01   Request from 1.1.1.1 for A-record for www.website1.com
00:00:01   Sending reply to 1.1.1.1 about A-record for www.website1.com:
00:00:01   -> Answer: A-record for www.website1.com = 999.999.999.999
00:00:02   Request from 1.1.1.1 for A-record for www.website2.com
00:00:02   Plug-in "Domain Blacklist1" matched A-record for www.website2.com
00:00:03   Request from 1.1.1.1 for A-record for www.website3.com
00:00:04   Sending reply to 1.1.1.1 about A-record for www.website3.com:
00:00:01   -> Answer: A-record for www.website3.com = 888.888.888.888

I only want to enter the data from the 4th line because the 5th line has "Domain Blacklist" in it.

Any thoughts on how I might go about that?

Paul

1

There are 1 answers

9
SOS On

Use File functions to loop through the file, one line at a time. Use a separate variable to keep track of the "previous" line (updating it at the end of each iteration). Within the loop, check the current line for the magic phrase. If it's found, append the previous line variable to a result array. When finished, use the array of lines however you wish.

<cfscript>
   matched = [];
   previousLine  = "";
   theFile  = fileOpen("c:/path/to/your/file.txt");

   try {
       while(!fileIsEOF(theFile))  {
           currentLine = fileReadLine(theFile);

           // if phrase is found in *this* line, store *previous* line 
           if (reFindNoCase("(Domain Blacklist)", currentLine)) {
               arrayAppend(matched, previousLine );
           }

           // update previous line
           previousLine  = currentLine;
       }
   }
   finally {
       theFile.close();
   }

   // do something with the results
   writeDump(var=matched, label="Matched Lines");
</cfscript>