Hi,
Today I will explain you the program which will help you read whole log file or last given number of lines in a file.
use strict;
my $argc = scalar(@ARGV);
# validate command line arguments
if ($argc eq 0)
{
print "Error :: Log file not provided\n";
print "\nUsage\n\n";
print "$0 \n";
print "where,\n";
print "log file name = Fully qualified name of the log file\n";
print "number of last records = Optional parameter. Specify the number of last records you want to read\n";
exit;
}
my $log_file = $ARGV[0];
my $number_of_records = $ARGV[1];
if (not defined $number_of_records)
{
$number_of_records = "";
}
unless (-e $log_file)
{
die "Error :: $log_file doesn't exist\n";
}
# Define variables.
my $current_line = 0; # determine current position in file
my $seek_line = 0; # determines line number where we want to jump and start reading
# Get total number of lines from the file
open (FI, $log_file) or die $!;
while ()
{}
my $lines = $.;
close(FI);
print "\nTotal number of lines in file = $lines\n";
if ($number_of_records ne "")
{
$seek_line = $lines - $number_of_records;
}
print "Seeking line number $seek_line\n";
open (LOG, $log_file) or die $!;
while ()
{
my $str = $_;
$current_line++;
next if ($current_line <= $seek_line);
chomp ($str);
print "line number $current_line: $str\n";
}
close(LOG);
Today I will explain you the program which will help you read whole log file or last given number of lines in a file.
use strict;
my $argc = scalar(@ARGV);
# validate command line arguments
if ($argc eq 0)
{
print "Error :: Log file not provided\n";
print "\nUsage\n\n";
print "$0
print "where,\n";
print "log file name = Fully qualified name of the log file\n";
print "number of last records = Optional parameter. Specify the number of last records you want to read\n";
exit;
}
my $log_file = $ARGV[0];
my $number_of_records = $ARGV[1];
if (not defined $number_of_records)
{
$number_of_records = "";
}
unless (-e $log_file)
{
die "Error :: $log_file doesn't exist\n";
}
# Define variables.
my $current_line = 0; # determine current position in file
my $seek_line = 0; # determines line number where we want to jump and start reading
# Get total number of lines from the file
open (FI, $log_file) or die $!;
while (
{}
my $lines = $.;
close(FI);
print "\nTotal number of lines in file = $lines\n";
if ($number_of_records ne "")
{
$seek_line = $lines - $number_of_records;
}
print "Seeking line number $seek_line\n";
open (LOG, $log_file) or die $!;
while (
{
my $str = $_;
$current_line++;
next if ($current_line <= $seek_line);
chomp ($str);
print "line number $current_line: $str\n";
}
close(LOG);