Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a list to find a string in another file.

Status
Not open for further replies.

Flipster

MIS
Jun 25, 2001
38
0
0
US
I have a list of items in one file (ITEMS). I need to loop through this list of items and search another file(LOGFILE) to see if the item string is anywhere in the log file.

For speed's sake it only needs to find one instance of the item string and kick out. So whether it is in the logfile once or three thousand times does not matter.

I would like to output whether the account string was found or not found.

Do I open the item list file and enter into a while loop, and then within the while loop search the file? I'd rather keep it within Perl rather than going out to the shell to grep for it. Is there an easy way to "load" the whole logfile (60,000 lines) only once while doing the search? Ther are many examples of a search but the search criteria is always a fixed single element. Any help would be appreciated.
 
Hi

Flipster said:
I'd rather keep it within Perl rather than going out to the shell to grep for it.
So you want to avoid this ?
Code:
[b]print[/b] [green][i]'something found'[/i][/green] [b]unless[/b] [b]system[/b] [green][i]'grep -q -f ITEMS LOGFILE'[/i][/green][teal];[/teal]
I somehow agree. However log files tend to be huge. And when searching huge files, you may have to put some effort to keep up with [tt]grep[/tt]'s speed.
Flipster said:
Is there an easy way to "load" the whole logfile (60,000 lines) only once while doing the search?
That should be the last thing to do. Load the ITEMS into and array, then loop over the LOGFILE lines once, checking each previously loaded array item against the current log line in a second loop.

Feherke.
 
Can you supply a couple examples of your log entries? It might be possible to store all the ITEMS in a hash and separate out the ITEM from each of the log file entries.

Comparing the ITEM from the LOGFILE record against a hash is going to be faster than going through the entire ITEMS array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top