Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Do you know where your variables are?
 
PerlMonks  

Re: Need a script to identify a given string in a log file

by monarch (Priest)
on Nov 10, 2008 at 05:42 UTC ( #722557=note: print w/ replies, xml ) Need Help??


in reply to Need a script to identify a given string in a log file

My recommended approach:

  • find all log files you wish to search (e.g. messages, messages.1, messages.2, and so forth)
  • order your list of log files from the most recent to the oldest, say in an array
  • for each file in your list search for your string
  • if you find your string, store it, but keep reading through the file in case you find a later occurrence
  • return the last found string, but if you didn't find the string then continue on to the next newest log file

For ordering your files you could possibly do the following:

my @files = glob( "/var/log/messages*" ); # sort by mtime my @sortedfiles = sort { ( stat($b) )[9] <=> ( stat($a) )[9] } @files;

You could, of course, attempt to start from the tail of the log file and work your way backwards, but that is very hard work and may be an unnecessary optimisation.


Comment on Re: Need a script to identify a given string in a log file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://722557]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-04-21 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls