Bala
Bala

Reputation: 390

how to extract data from log file in perl

Im new to perl and want to extract information (NAME,DESCR,PID,VID,SN) from a log file for usage. below is sample of one entry in the log file.

NAME: "data1023", DESCR: "some information"
PID: ABC-0123-xyz      , VID: V01 , SN: ABC1234567

i tried using split using comma as delimiter but its not helping much. could some one suggest a better approach to this problem?

Upvotes: 0

Views: 1084

Answers (1)

Hameed
Hameed

Reputation: 2277

You haven't given us much but based on some assumptions, including but not limited to 2-lins per entry, here is quick solution that you can build upon to your liking.

#!/usr/local/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $lineno;
my @parts;
my $entryno;
my $line;
my @log;

while (<>) {
    $line = $_;
    chomp $line;
    $lineno++;
    if ( $lineno % 2 ) {

        #It is line one of the entry
        $entryno++;
        @parts = split( /,\s*/, $line );
    }
    else {
        push( @parts, split( /,\s*/, $line ) );

        push( @log, [@parts] );
    }
}

print Dumper(\@log);

It all depends on how you want the data to be presented. All this does is puts every element of each entry as one array item and then each entry as an array item, building an array of arrays.

Upvotes: 3

Related Questions