How can I read a continuously updating log file in Perl?

Posted by Octopus on Stack Overflow See other posts from Stack Overflow or by Octopus
Published on 2010-05-14T05:49:49Z Indexed on 2010/05/17 5:40 UTC
Read the original article Hit count: 189

Filed under:

I have a application generating logs in every 5 sec. The logs are in below format.

11:13:49.250,interface,0,RX,0
11:13:49.250,interface,0,TX,0
11:13:49.250,interface,1,close,0
11:13:49.250,interface,4,error,593
11:13:49.250,interface,4,idle,2994215
and so on for other interfaces...

I am working to convert these into below CSV format:

Time,interface.RX,interface.TX,interface.close....
11:13:49,0,0,0,....

Simple as of now but the problem is, I have to get the data in CSV format online, i.e as soon the log file updated the CSV should also be updated.

What I have tried to read the output and make the header is:

#!/usr/bin/perl -w
use strict;

use File::Tail;
my $head=["Time"];
my $pos={};
my $last_pos=0;
my $current_event=[];

my $events=[];

my $file = shift;
$file = File::Tail->new($file);

while(defined($_=$file->read)) {
     next if $_ =~ some filters;

     my ($time,$interface,$count,$eve,$value) = split /[,\n]/, $_;
     my $key = $interface.".".$eve;

     if (not defined $pos->{$eve_key}) {
          $last_pos+=1;
          $pos->{$eve_key}=$last_pos;
          push @$head,$eve;
        }
        print join(",", @$head) . "\n";
}

Is there any way to do this using Perl?

© Stack Overflow or respective owner

Related posts about perl