#!/usr/bin/perl -w # Simple program to read the last n line(s) of a file. # Reads from the end of the file for effeciency # "\n" linux only, # usage tailz filename numberoflines use strict; my $filename = shift or die "Usage: $0 file numlines\n"; my $numlines = shift; my $byte; # Open the file in read mode open FILE, "<$filename" or die "Couldn't open $filename: $!"; # Rewind from the end of the file until count of eol 's seek FILE,-1, 2; #get past last eol my $count=0; while (1){ seek FILE,-1,1; read FILE,$byte,1; if(ord($byte) == 10 ){$count++;if($count == $numlines){last}} seek FILE,-1,1; if (tell FILE == 0){last} } $/=undef; my $tail = <FILE>; print "$tail\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: pure perl tail
by merlyn (Sage) on Sep 06, 2002 at 20:41 UTC | |
by Spenser (Friar) on Sep 06, 2002 at 22:19 UTC | |
by belg4mit (Prior) on Sep 06, 2002 at 22:28 UTC | |
|
Re: pure perl tail
by belg4mit (Prior) on Sep 06, 2002 at 21:51 UTC | |
|
Re: pure perl tail
by zentara (Cardinal) on Sep 07, 2002 at 16:04 UTC | |
by suyashjain (Initiate) on Apr 26, 2013 at 16:58 UTC | |
|
Re: pure perl tail
by Aristotle (Chancellor) on Sep 07, 2002 at 19:24 UTC |