Hello ytjPerl,

There is an alternative solution to your problem using module File::ReadBackwards

Sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use File::ReadBackwards; my $numlines = 20; my $filename = 'in.log'; my $bw = File::ReadBackwards->new($filename) or die "can't read $filename $!"; my @lines; my $count = 0; while( defined( my $line = $bw->readline ) ) { chomp $line; push @lines, $line; $count++; last if $count == $numlines; } @lines = reverse @lines; print Dumper \@lines; __END__ $ perl reverse_file.pl $VAR1 = [ 'line 2', 'line 3', 'line 4', 'line 5', 'line 6', 'line 7', 'line 8', 'line 9', 'line 10', 'line 11', 'line 12', 'line 13', 'line 14', 'line 15', 'line 16', 'line 17', 'line 18', 'line 19', 'line 20', 'line 21' ];

Sample of in.log for testing purposes:

line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 line 20 line 21

As a next step you can apply a grep or a regex on the lines that you have collected and do what ever you want with the process.

Update: I would use flock to lock the file as it is important not get mixed on an instance that data are populated on the file. Sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use File::ReadBackwards; # import LOCK_* and SEEK_END constants use Fcntl qw(:flock SEEK_END); sub lock { my ($fh) = @_; flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n"; # and, in case someone appended while we were waiting... seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n"; } sub unlock { my ($fh) = @_; flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n"; } my $numlines = 20; my $filename = 'in.log'; my $bw = File::ReadBackwards->new($filename) or die "can't read $filename $!"; my $fh = $bw->get_handle(); lock($fh); my @lines; my $count = 0; while( defined( my $line = $bw->readline ) ) { chomp $line; push @lines, $line; $count++; last if $count == $numlines; } unlock($fh); $bw->close(); @lines = reverse @lines; print Dumper \@lines; __END__ $ perl reverse_file.pl $VAR1 = [ 'line 2', 'line 3', 'line 4', 'line 5', 'line 6', 'line 7', 'line 8', 'line 9', 'line 10', 'line 11', 'line 12', 'line 13', 'line 14', 'line 15', 'line 16', 'line 17', 'line 18', 'line 19', 'line 20', 'line 21' ];

Update2: In case you want to loop again and again the file in reverse mode you can apply something like that. Sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use File::ReadBackwards; # import LOCK_* and SEEK_END constants use Fcntl qw(:flock SEEK_END); sub lock { my ($fh) = @_; flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n"; # and, in case someone appended while we were waiting... seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n"; } sub unlock { my ($fh) = @_; flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n"; } sub check_file { my ($file, $numlines) = @_; my $bw = File::ReadBackwards->new($file) or die "can't read $file $!"; my $fh = $bw->get_handle(); lock($fh); my @lines; my $count = 0; while( defined( my $line = $bw->readline ) ) { chomp $line; push @lines, $line; $count++; last if $count == $numlines; } unlock($fh); $bw->close(); return reverse @lines; } my @data; my $numlines = 20; my $process = "line 2"; my $filename = 'in.log'; while ( 1 ) { @data = check_file($filename, $numlines); last if (grep /$process/, @data); sleep 5; } print Dumper \@data; __END__ $ perl reverse_file.pl $VAR1 = [ 'line 2', 'line 3', 'line 4', 'line 5', 'line 6', 'line 7', 'line 8', 'line 9', 'line 10', 'line 11', 'line 12', 'line 13', 'line 14', 'line 15', 'line 16', 'line 17', 'line 18', 'line 19', 'line 20', 'line 21' ];

Update3 Last Update: Similar solution different way of writing it, just for fun and preference. Sample of code bellow:

do { @data = check_file($filename, $numlines); sleep 5; } while ( grep not /$process/, @data );

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: how to populate array with last 20 lines of file by thanos1983
in thread how to populate array with last 20 lines of file by ytjPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.