0

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?

4
  • Can you specify the format of the output more? eg. What does an SN look like? Can it start with a number? Commented Jul 17, 2012 at 5:58
  • 1
    Please give some additional information: 1: Are these two separate lines or is the data contained in one line? 2: Are the double quotes present in the file? Commented Jul 17, 2012 at 6:01
  • 1
    That's really some messy data. Did you copy and paste it, or type it in by hand? I ask because there's no comma after the DESCR field, there's whitespace before the comma following the PID, and before the comma following VID. Those could be fixed-width fields, I suppose, but it just looks several possible typos. Commented Jul 17, 2012 at 6:28
  • its a system generated log. both line put together form one entry. double quotes are present in the log. i want all the data to be accessible as a string for usage later. Commented Jul 17, 2012 at 8:49

1 Answer 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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

open(FH, "<file.txt"); and use it as while(<FH>) for reading the file rite?
Right! You could do that. It is better to use 3-argument open statement like this open(my $fh, "<", "file.txt") and use it as while(<$fh>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.