Junaid Najamuddin wrote:
>
> Hi,
Hello,
> 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
>
> thanks
> junaid
>
> open(INFILE, "$File");
You should _always_ verify that the file was opened.
open INFILE, $File or die "Cannot open $File: $!";
> $size = @lines = (<INFILE>);
> close (INFILE);
If you just want the last ten lines of the file then:
my $index;
my @lines;
while ( <INFILE> ) {
$lines[$index++] = $_;
$index %= 10;
}
splice @lines, 0, 0, splice @lines, $index;
> $cnt = 0;
> $tail = 10;
> foreach (@lines)
If you _are_ reading the entire file into @lines you can loop over the
last ten lines like this:
foreach ( @lines[-10 .. -1] )
> {
> if (($cnt >($size - $tail)) and (/\berror\b/i))
^^^^^^^^^^^^^^^^^^^^
Not required if you use either of the modifications I suggested.
> {
> 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;
> }
> }
> }
I don't see anything wrong with the if-else block.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]