develooper Front page | perl.beginners | Postings from January 2007

Re: Read a text file starting from the bottom

Thread Previous | Thread Next
Rob Dixon
January 15, 2007 04:02
Re: Read a text file starting from the bottom
Message ID:
Michael Alipio wrote:
> I wanted to extract the start time of a particular logfile (located at the
> beginning of the file, and also the end time which is located at the end of
> the logfile.
> Is there any way to do this in perl without using "head" or "tail" commands?

To read a file backwards, you could make use of File::ReadBackwards. The code
below assumes that you log file is called 'logfile'.

use strict;
use warnings;

use File::ReadBackwards;

my $fh = File::ReadBackwards->new('logfile');

my $last_line = $fh->readline;

 > By the way, can you point me somewhere perhaps a perl function that lets you
 > create directories?

To create a directory use the mkdir() built-in function. It may be more
convenient to use the File::Path module, which provides a mkpath() function
which will also create any intermediate necessary to form a complete path. (It
also offers an rmtree() function.)



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About