Last line of file/Perl

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Kevin Buettner
Date:  
Subject: Last line of file/Perl
On Nov 14, 3:39pm, Mike Starke wrote:

> I need to open a file and just read in the last line, can anyone
> throw me a bone?


Below is a simple program which will sort of do what you want. It'll
print the last line of the file, but it'll read all of the other lines
to get there. To really do what you want, you'll need to seek to the
end and then seek backwards reading each character until you find the
end-of-line character(s) of the penultimate line.

#!/usr/bin/perl

my $l;

while (<>) {
    $l = $_;
}


print $l;