Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Grab last line and print

by Anonymous Monk
on Aug 25, 2006 at 18:25 UTC ( [id://569662]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to open up a file and fetch the last line only:
Friday, 08/25/2006 13:56 Friday, 08/25/2006 13:57 Friday, 08/25/2006 14:10 Friday, 08/25/2006 14:15
In the above file I would want: Friday, 08/25/2006 14:15

I can open the file but how would I fetch the last line??
my $file = 'oratabtest.txt' open FILE, "< $file" or die; my @lines = <FILE>; open FILE, "> $file" or die; foreach (@lines) { if (#something to fetch last line) { print $lastline; } } print "here is last line: $lastline";

Replies are listed 'Best First'.
Re: Grab last line and print
by Not_a_Number (Prior) on Aug 25, 2006 at 18:44 UTC

    1) If the file is short:

    eof and print while <$file>;

    2) If the file is long, check out File::ReadBackwards

      Thanks it works great.

      I have been on many many forums and this Perl Monks is by far the best. Everytime I have posted, I get multiple responses and guidance form experts in a very quick amount of time!

      Thanks again!!!
Re: Grab last line and print
by jwkrahn (Abbot) on Aug 25, 2006 at 18:32 UTC
    Since you already have the contents of the file stored in an array:
    print "here is last line: $lines[ -1 ]";
Re: Grab last line and print
by madbombX (Hermit) on Aug 25, 2006 at 18:36 UTC
    Check out the CPAN module File::ReadBackwards.

    use File::ReadBackwards; my $bw = File::ReadBackwards->new( 'oratabtest.txt' ) or die "can't read 'oratabtest.txt' $!" ; while( my $line = $bw->readline) { print "Last line: $line"; last; }

    Eric

Re: Grab last line and print
by radiantmatrix (Parson) on Aug 25, 2006 at 20:14 UTC

    The "short path" is this:

    open FILE, '<', $file or die("Can't read $file: $!"); my $lastline; while (<FILE>) { $lastline = $_; }

    Of course, you're still reading through the whole file, but at least you're not storing its whole contents in memory... as others have said, though, File::ReadBackwards is probably your best bet.

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: Grab last line and print
by planetscape (Chancellor) on Aug 26, 2006 at 07:46 UTC
Re: Grab last line and print
by derby (Abbot) on Aug 25, 2006 at 18:32 UTC

    so close ... so close

    my $file = 'oratabtest.txt' open FILE, "< $file" or die; my @lines = <FILE>; open FILE, "> $file" or die;

    Now the question is not how to get the last line of a file but the last element of an array (since you slurped the whole file into @lines).

    -derby
Re: Grab last line and print
by Jenda (Abbot) on Aug 25, 2006 at 18:36 UTC

    ad 1) If you want to get the last element of an array you can use the index -1 or $#array_name: print $lines[-1]; or print $lines[$#lines];

    ad 2) if you are only interested in the last line you don't need to store the whole file in an array. Just loop through the file and remember the line you've just read, until you arrive at the end and print the last line you've seen.

    Of course if you know the file is huge you may need/want to do something more complex, using seek(), reading last N bytes of the file, checking there is a newline character in that and possibly read a bigger chunk ... but the question is whether it's worth the effort.

Re: Grab last line and print
by izut (Chaplain) on Aug 28, 2006 at 10:54 UTC

    I see no mention to Tie::File. With this module, you can use your file as a Perl array.

    use Tie::File; tie @array, 'Tie::File', $filename or die $!; print $array[-1];

    But remember: this will open your file in read/write mode, so any modifications you make in array will reflect on file's data. Use this way for read-only access:

    use Tie::File; use Fcntl 'O_RDONLY'; tie @array, 'Tie::File', $filename, mode => O_RDONLY or die $!;

    Igor 'izut' Sutton
    your code, your rules.

Re: Grab last line and print
by o2bwise (Scribe) on Aug 25, 2006 at 18:32 UTC
    Hi,

    I am pretty sure the following would work (assuming a unix or linux platform):

    $lastLine = `tail -1 <fileName>`;
    It just uses the linux tail function. I know windows has a tail-like function that you can download.

    Tony

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://569662]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2024-04-25 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found