Monday, April 28, 2014

Reading last few lines from a log file using perl

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);



2 comments:

  1. When do we use this ? tail command on linux also serves the same purpose right ?

    ReplyDelete
    Replies
    1. As you pointed out Tail command serves the purpose but this program shows programmatic way to do the same.

      This program is important when you wanted to parse the last few lines of the file and do some operation on them. For Example: I was having log file which got generated as a result of user addition in db along with time taken to add this user. I had logs of millions of records in single log file. I just wanted to get average time of last 1000 Records. Similarly many other operations can be done while parsing upside down.

      Delete