"Junaid Najamuddin" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED].
net...
> Hi,
>
> Thanks in advance, if someone can help me out
> I am trying to read the last 10 lines of a log file. If the script finds a
> word ERROR it should email and log the event. If not then do nothing.
> Some how the other first part is working
> If it finds the word ERROR  it does  what needs to be done but when it do
> not find the word it doesn't do anything
> I am about to pull my hair.
>
> Can some one help me please
>

in your code you exit() the program after the first line that is checked
wether or not the line triggers an error. Ath this point the only line that
gets checked is the 10th to the last line. Your spec says to check the last
10 lines.

I'd probably use something that looks like this:

open(INFILE, "$File");
@lines = (<INFILE>);
close (INFILE);

for my $index ($#lines - 10 .. $#lines) {
  if( $lines[ $index ] =~ m/\berror\b/ ) {
    print("found error, element: $index");
    print(" value: $lines[ $index ]\n");

> print LOG "xyz log has a problem\n";
> &email ("\nxyz log has a problem on $t\n");

  } else {
    print("element: $index okay, val= $lines[ $index ]\n");

> print "I am doing great";
> print LOG "xyz is working fine\n";
> print LOG "No E-mail is being sent\n";

  }
}

dont use exit().........


> open(INFILE, "$File");
> $size = @lines = (<INFILE>);
> close (INFILE);
> $cnt = 0;
> $tail = 10;
> foreach (@lines)
> {
> if (($cnt >($size - $tail)) and (/\berror\b/i))
> {
> print "$_";
> $cnt++;
> print LOG "xyz log has a problem\n";
> &email ("\nxyz log has a problem on $t\n");
> exit;
> }
>
> else
> {
> print "I am doing great";
> print LOG "xyz is working fine\n";
> print LOG "No E-mail is being sent\n";
> close LOG;
> exit;
> }
>
> }
> }
>
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to