GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  Perl
Go To First  |  Previous Question  |  Next Question 
 Perl  |  Question 43 of 61    Print  
I was asked how to implement the unix tail command in perl
$ tail.pl t.txt 10 (display last 10 lines)
I kind of managed with the simple ARRAY and they playing around with the $#Array etc but he wanted not without an array
Any inputs ?

  
Total Answers and Comments: 6 Last Update: November 02, 2009     Asked by: vm 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
June 21, 2007 05:22:13   #1  
geek2007 Member Since: June 2007   Contribution: 1    

RE: I was asked how to implement the unix tail command...
you can use the following shell command in your perl script .

`cat filename|tail -10f ;`

 
Is this answer useful? Yes | No
October 25, 2007 06:00:00   #2  
Guest        

RE: I was asked how to implement the unix tail command...
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.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
May 13, 2008 13:16:19   #3  
imavasu Member Since: April 2008   Contribution: 3    

RE: I was asked how to implement the unix tail command in perl $ tail.pl t.txt 10 (display last 10 lines) I kind of managed with the simple ARRAY and they playing around with the $#Array etc but he wanted not without an array Any inputs ?
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";
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
June 10, 2008 03:16:37   #4  
rahulashu Member Since: June 2008   Contribution: 4    

RE: I was asked how to implement the unix tail command in perl $ tail.pl t.txt 10 (display last 10 lines) I kind of managed with the simple ARRAY and they playing around with the $#Array etc but he wanted not without an array Any inputs ?

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 <MYFILE> ;
#print @array ;
$numline @array;
$numprintline $numline - $ARGV[1] ;
while($numprintline ! $numline)
{
print "$array[$numprintline]" ;
$numprintline++;
}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 16, 2008 00:24:50   #5  
nitashaa Member Since: July 2008   Contribution: 9    

RE: I was asked how to implement the unix tail command in perl $ tail.pl t.txt 10 (display last 10 lines) I kind of managed with the simple ARRAY and they playing around with the $#Array etc but he wanted not without an array Any inputs ?
#!/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; }
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
November 02, 2009 16:24:59   #6  
lbsign Member Since: November 2009   Contribution: 1    

RE: I was asked how to implement the unix tail command in perl $ tail.pl t.txt 10 (display last 10 lines) I kind of managed with the simple ARRAY and they playing around with the $#Array etc but he wanted not without an array Any inputs ?
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 (<FILE>) {
print;
}

close(FILE);

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape