Re: removeheaders

"Mike Cappella" <[email protected]>
Newsgroups gmane.comp.log.logwatch.devel
Message-ID <000201c7b7a2$7b563260$0a02a8c0@yellowstone>
> > I don't know if performance is at all a concern with this small 
> > script, but in its current state it is probably running
> > 3 times slower than it needs to be. It compares the line to three 
> > regular expressions, even though only one should ever match. The 
> > expressions should also be ordered such that the most 
> likely ones are 
> > tried first.
> > 
> > The diff below corrects these issues. I've only tested it with data 
> > that matches the first expression.
> > 
> > Also, in this line:
> > 
> >   while (defined($ThisLine = <STDIN>)) {
> > 
> > is defined() doing anything useful? Even a blank line, due to the 
> > newline, will still return true.
> > 
> >   -Tom
> > 
> 
> 

Tom,

I think the removeheaders optimization you posted can be further reduced,
potentially avoiding calling the RE generator twice for non-matching lines,
and avoids traversing the same sub-expression twice for matching lines.

# Removes the beginning of each line of a standard /var/log/messages-style
# logfile.
#
# Assumptions: OnlyService is called before RemoveHeaders

while (<>)) {
   if (s/^... .. ..:..:.. [^ ]* //) {       # Strip date, time, service
      if (s/^[^\[:]*(?:\[\d*\])?: //) {     # ":" after service name may not
exist
         s/^\[ID \d+(?: \w+\.\w+)?] //;     # Strip Solaris ID tag style
-mgt
      }
   }
   print;
}

MrC
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.