Edward WIJAYA wrote: > On Mon, 25 Jul 2005 21:39:50 +0800, <[EMAIL PROTECTED]> wrote: > > >> Can someone suggest a perl command line snippet that will print the >> last n >> lines of a file. > > > If you are under unix/linux > just use "tail -n" command. > > However if you really want to go via Perl command line: > > $perl -e ' > open FH, "<file" or die $!; > @lines = <FH>; > close FH or die $!; > foreach $i (($#lines - $n) .. $#lines) { print "$lines[$i]\n";} ' >
I shortened this up a bit: perl -e 'open FH, "$filename"; @lines=reverse <FH>; print $lines[$_] foreach reverse 0..$n;' Where "$filename" is replaced with the name of the file and "$n" is replaced with the number of lines to display. Hope that helps! --Errin -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>