I have tried writing a Perl script to simulate Linux tail command - print last n lines from a file. This is one of the common interview questions in Perl. This can be extended to print selected lines of other Linux commands like head. It works perfectly fine.
print "\nImplements linux tail command in perl \n\n";
my $file_name =shift; open(FILE, "$file_name") || die "Can't open file. Error: $!\n"; my @arr = ; close(FILE); my $index=scalar(@arr)-$maxline; print @arr[$index .. $#arr];
Working as a software engineer, started blogging to share the information. My posts usually includes, tips and tricks on daily computer use, and how to series on MS Office.
3 Comments
how about implementing tail -f after this :)
ReplyDeletewhile(1){
$prev_size = -s $file;
tailf($file,10) if (-s $file > $prev_size);
}
tailf() will be your written code with little updates :)
We can also write tail implementation in Perl as below
ReplyDeletemy $maxline = 10;
if(scalar(@ARGV) == 2){
$maxline = shift;
}
my $file_name =shift;
open(FILE, "$file_name") || die "Can't open file. Error: $!\n";
my @arr = ;
close(FILE);
my $index=scalar(@arr)-$maxline;
print @arr[$index .. $#arr];
gümüşhane
ReplyDeletebilecik
erzincan
nevÅŸehir
niÄŸde
İR5GN