Last line of file/Perl

Kevin Buettner plug-discuss@lists.plug.phoenix.az.us
Thu, 14 Nov 2002 13:50:34 -0700


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;