
Reading Last X Lines of File?
Is there an easy/straight forward way to read the last X lines
(from 1-4) lines of a text file?
I can read the last line by:
while (<>) {
$LastLine = $_;
}
but how do I capture the last 2-4 lines?
How about:
open(LASTLINES, "tail -4 $file") || die "Can't open file: $!\n";
while (<LASTLINES>) {
# do whatever to your file lines
Quote:
}
or you can slurp the file into an array (means you can work with other
parts also....
while (<>) {
Quote:
}
This last one is probably only good if you want to use the other lines for
something, or if it's a relatively small file. If the file is of any
noticable size, you might prefer the first method.
--
University of Florida, Department of Computer and Information Sciences
smail: Stephen P Potter, E309A CSE, UF Campus
Click here to find out about the <a href="http://www.cis.ufl.edu/perl">Perl World Wide Web Page</a>