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 ?
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.
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"; }
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 :
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; } }
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++ } }