简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM