in reply to extract last 1 min log and find a string

I am think search the string from bottom line, I know I could use File:ReadBackwards, but how to achieve that when I find the first occurrence string, stop the search and print "found" out. My code below repeat print out "found" more than 4 times, but I already set $count = 4, please advice, thanks
use File::stat; use Time::localtime; my $fh = "/daily20170718120401.log"; my $target = "48 processes started"; my $count == 0; while ( $count< 4){ my $timestamp = ctime(stat($fh)->mtime); print "$timestamp\n"; sleep(30); my $timestamp2 = ctime(stat($fh)->mtime); print "$timestamp2\n"; if ($timestamp eq $timestamp2) { my $bw = File::ReadBackwards->new("backwards.txt") or die $!; my $line; while (defined($line = $bw->readline)) { chomp $line; if ($line =~ /target/) { print "found\n"; $count = 4; } } $bw->close(); } } else{print "changed\n"; $count += 1;} }

Replies are listed 'Best First'.
Re^2: extract last 1 min log and find a string
by poj (Abbot) on Jul 24, 2017 at 19:38 UTC
    My code below repeat print out "found" more than 4 times

    That's odd, because I get

    syntax error at 1195905.pl line 31, near "else"
    syntax error at 1195905.pl line 31, near ";}"
    Unmatched right curly bracket at 1195905.pl line 33, at end of line
    1195905.pl had compilation errors.
    
    poj
      Hi Dude, I just copied an extra "}", why not fix it at your end?
        my $count = 0; # not $count == 0
        while (defined($line = $bw->readline)) { chomp $line; if ($line =~ /$target/) { # why target ? print "found\n"; $count = 4; last; # <-- add } }
        poj