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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Looking for Errors'

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
My perl scripts would need to examine a log file. I would like to raise an error to user when
1) On any of the lines, the word Error or Warn occurs on its own (i.e. would not count
Code:
Without errors
or
Code:
Without warnings

How could I do that? My best guess is
Code:
$outputStr =~ /Error/i or $outputStr =~ /Warn/i) and
         !($outputStr =~ /without Error/i and 
           $outputStr =~ /without Warng/i))

But it does something else instead :)
 
You could use 'look aheads' with the pattern matching, but, it gives me a headache every time I have to figure out how to make that work( and it usually takes several tries to make it work correctly). A 'not as elegant' approach would to be skip over valid lines and raise errors on 'Error' or 'Warn' lines.

while ($string = <INFILE>)
{
if ($string =~ /without/) { next; } # read next line of input
if ($string =~ /Error|Warn/i) { print &quot;ERROR => $&\n&quot;; } # print Error to user
}

HTH


keep the rudder amid ship and beware the odd typo
 
Thanks, but I already have the $outputStr as a String, any regular expressions constructs I could use to search line by line?
 
Here's another *untested* option:

while (<INFILE>) {
if (/(\w+) error|warning/i) {
if (!$1 || $1 ne &quot;without&quot;) {
print &quot;This is an Error\n&quot;;
}
}
}
Hardy Merrill
Mission Critical Linux, Inc.
 
I don't understand the question.
Do you mean that the entire content of the log file is in the var, $outputStr?
Or, are you getting the lines one at a time through a file handle as $outputStr?
I need a little more context.
Can you post the code you are using to set $outputStr?

THX


keep the rudder amid ship and beware the odd typo
 
the problem is to look for lines that have NOTHING on them but the word 'error' or the word 'warn', so say that:[tt]
m/^(error|warn)$/i[/tt]


you also asked about how to search line by line. you could do this when you initially read in the file, with a:[tt]
while(<FILE>)
{
&throw_error if (m/^(error|warn)$/i);
$string .= $_;
}[/tt]

you can only really do a line by line search if the file is first split up in a line by line fashion. if you read the file into an array, you could do a foreach on that array that would look pretty much like the while loop above. you could always split the string up on newlines, but all of this would be alot of extra work.
if it's already in a string, you could do a global multi-line search that would look a little like this ($\ is probly just '\n'):[tt]
while ($string =~ m/${\}(error|warn)${\}/igm)
{
&throw_error;
}[/tt]

both of these constructs are built to make one error for every match they make, but if you only need to make one error no matter how many times it happens, you can just do this:[tt]
&throw_error if (m/${\}(error|warn)${\}/im);[/tt]


i'm not sure if any of these are the answers you're looking for. if they aren't please restate the question. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top