you can use the following shell command in your perl script .
`cat filename|tail -10f ;`
Login to rate this answer.
Guest
Answered On : Oct 25th, 2007
This may not be accepted as well. But this can be done in numerous ways ! One way is here :
maintain a structure which stores the line number and the size of the file at that time.
eg 1 - 10bytes, 2 - 18 bytes ... you'll have to have a counter to increase the number of lines to find out the number of lines in the file. once you are through the file, you'll know the size of the file at any nth line. use sysseek to move the file pointer back to that position ( last 10 )and then start reading till the end.
Login to rate this answer.
Please try this capan module
use File::Tail;
$file=File::Tail->new("/some/log/file");
while (defined($line=$file->read)) {
print "$line";
}
use File::Tail;
$file=File::Tail->new(name=>$name, maxinterval=>300, adjustafter=>7);
while (defined($line=$file->read)) {
print "$line";
}
Login to rate this answer.
if we will passed 2 argument , in that first is filename and second is no. of last line then u can use following program :
#! /bin/perl
open(MYFILE,$ARGV[0]);
@array = ;
#print @array ;
$numline = @array;
$numprintline = $numline - $ARGV[1] ;
while($numprintline != $numline)
{
print "$array[$numprintline]" ;
$numprintline++;
}
Login to rate this answer.
#!/usr/bin/perl
my $count = $ARGV[1]; ---> the number of lines to be printed
while(<>) { ----> reading file passed as arg
my $x = $.; -----> assigning line number to $x
if ( $count >= $x )
{ print "line number $x: $_n"; }
else { exit; }
}
Login to rate this answer.
I think the point to not using arrays is the fact that loading large files in to an array is expensive.
$times = 10; # this is the number of lines
open(FILE, "$file") or die ("...");
seek(FILE, 0,2);
my $counter =0;
while ($counter < $times && seek(FILE, -2,1)) {
read(FILE, $chr, 1);
if ($chr eq "n") { $counter++ }
}
while () {
print;
}
close(FILE);
Login to rate this answer.
How about opening the file twice and get two filehandles, and then navigate:
#!/usr/bin/env perl
# Usage: $0
use warnings;
use strict;
open(FILE1,"$ARGV[0]") or die $!;
open(FILE2,"$ARGV[0]") or die $!;
my $count = $ARGV[1];
while($count--) {
;
}
while() {
;
}
print ;
close(FILE1);
close(FILE2);
Login to rate this answer.
cat | perl -e '$i = 0 ; map { print $_ if ($i++ < 10) } reverse (<>)'
Login to rate this answer.
#!/usr/bin/perl
if (@ARGV < 1) {
die ("Usage: $0 filename [number of lines]n");
}
my $fileName = $ARGV[0];
# defaults to 5 lines if not set
my $noLines = $ARGV[1] || 5;
# Guess how many characters are in a line
my $bytesInLine = 128;
my $bytesInLines = $bytesInLine * $noLines;
open (FILE, "$fileName") or die ("Cannot read $fileNamen");
seek(FILE, 0,2);
my $maxBytes = tell(FILE);
my @fileLines;
my $passCount=0;
my $atBeginFile = 0;
# We want to get the full first line so we get number of lines + 1
while (@fileLines < $noLines+1) {
if (!seek(FILE, -1 * $bytesInLines,1)) {
seek(FILE, 0,0);
$atBeginFile = 1;
}
@fileLines = ;
$bytesInLines *= 2;
$passCount++;
# you passed the beginning of the file
if ($atBeginFile) {
# if the requested lines is above the actual file change it
$noLines = @fileLines;
last;
}
}
my $startIndex = @fileLines - $noLines;
@fileLines = splice(@fileLines, $startIndex,$noLines);
print @fileLines;
print "Tailed $noLines lines in $passCount passesn";
close(FILE);
Login to rate this answer.
Try this
#!/usr/bin/perl
use strict;
use warnings;
open(FH,"FILENAME") or die "$!";
while ()
{
chomp;
}
print "n$.";
my $x= $. - 10;
close (FH);
open(FH,"FILENAME") or die "$!";
while ()
{
chomp;
if ($.>=$x)
{
print "$.->$_";
}
}
close (FH);
Login to rate this answer.
puneet agrawal
Answered On : Mar 29th, 2012
I think below answer is suitable.
Code
while ($line=<FILE>) {
}
";
$m=$ARGV[0];
for(;$m > 0;$m--){
Login to rate this answer.
SH
Answered On : Mar 29th, 2012
Open file, read and print until number of required lines is reached. Memory is saved by not slurping the entire file into an array.
Code
print "tailing file $fileName for $lines lines...
";
open FH
, $fileName or die "Could not open $fileName";
while ($l = <FH>) {
last if $lines == 0;
}
Login to rate this answer.
Code
#!/usr/bin/perl
$fileName = shift;
$lines = shift;
print "tailing file $fileName for $lines lines...
";
open FH
, $fileName or
die "Could not open $fileName";
while ($l = <FH>) {
push @ar, $l; --$lines;
last if $lines == 0;
}
while ($l = <FH>) {
push @ar, $l;
shift @ar;
}
close FH;
foreach (@ar) {
}
Login to rate this answer.
SumitRoy
Answered On : Sep 16th, 2012
You can also try this :
Create two pointers and make them $count apart.
Move one of the pointer to EOF and keep simultaneously moving the other pointer so that it is $count before.
Now use the second pointer to print the last $count lines.
Code
#!/usr/bin/env perl
use warnings;
use strict;
my $count = $ARGV[1];
#After this loop FILE1 points to line after $count
while($count--) {
my $line = <FILE1>;
;
}
#After this loop FILE1 points to EOF and FILE2 points $count before EOF
while(<FILE1>) {
my $line = <FILE2>;
;
}
#Print the last $count lines before EOF
while(<FILE2>){
}
Login to rate this answer.
shiva
Answered On : Nov 22nd, 2012
Just use .. opertor using array just check the length of the array minus with input give by the user
take a difference and do it sample code is below.
Code
#! /usr/bin/perl
my @arry=qw(1 2 3 4 5 6 7 8 9);
my $len=$#arry;
print "Enter the number";
$l=<stdin>;
$result=$len-$l;
$res=@arry[$len] -@arry[$result];
foreach my $i (@arry[$result+1].. @arry[$len])
{
";
}
Login to rate this answer.