如何从perl中的日志文件中提取数据

时间:2021-10-19 08:11:00

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.

我是perl的新手,想要从日志文件中提取信息(NAME,DESCR,PID,VID,SN)以供使用。下面是日志文件中一个条目的示例。

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?

我尝试使用逗号分隔使用逗号作为分隔符,但它没有多大帮助。有人会建议更好地解决这个问题吗?

1 个解决方案

#1


3  

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.

您没有给我们太多但基于一些假设,包括但不限于每个条目2-lins,这里是您可以根据自己的喜好建立的快速解决方案。

#!/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.

这一切都取决于您希望如何呈现数据。所有这一切都是将每个条目的每个元素作为一个数组项,然后将每个条目作为数组项,构建一个数组数组。

#1


3  

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.

您没有给我们太多但基于一些假设,包括但不限于每个条目2-lins,这里是您可以根据自己的喜好建立的快速解决方案。

#!/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.

这一切都取决于您希望如何呈现数据。所有这一切都是将每个条目的每个元素作为一个数组项,然后将每个条目作为数组项,构建一个数组数组。