1

I have a simple problem (i think) extracting information from an archive in Perl. This archive has 200 000 lines approx and some lines has this format

WO      GB111MTD1                    dddD-51   TIME 141202 0105  PAGE  1

i want to put in a variable GB111MTD1, and i know that always the word "WO" is first.

What i have tried is the following:

open(archive, "C:/Users/g/Desktop/c/alarms.log") or die "blah\n";

while(< archive>){
        if($_ =~ /^WO\s+(.*)/){
            print "Found: $1\n";
            last;
        }
}

this prints me all the line, but i only want "GB111MTD1".

---next intent

while(< archive>){
        if($_ =~ /^WO\s+(.*)\S/){
            print "Found: $1\n";
            last;
        }
}

i want to say here "if the line begins with WO and have some whitespaces, match me what is next until other whitespace is found"

here, the only difference is that the "1" of WO GB111MTD1 dddD-51 TIME 141202 0105 PAGE 1 is not shown but still is not what i want

i hope you understand my problem.

1
  • An alternative approach is to regard this file as a CSV file and read it with Text::CSV or even SELECT your information with DBD::SQL. Commented Dec 12, 2014 at 7:14

2 Answers 2

4

You can use \S for non-whitespace characters:

use warnings;
use strict;

while (<DATA>) {
    if (/^WO\s+(\S+)/) {
        print "Found: $1\n";
        last;
    }
}

__DATA__
WO GB111MTD1 dddD-51 TIME 141202 0105 PAGE 1

Prints:

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

Comments

1

I would use split on lines that start with WO.

 use warnings;
 use strict;

 while (<DATA>) {
     if (/^WO\s/) {
         my @fields = split(/\s+/);
         my $myvar = $fields[1];
         # do stuff with $myvar
         say "Frobnicating order # $myvar";
     }
 }

 __DATA__
 WO GB111MTD1 dddD-51 TIME 141202 0105 PAGE 1

Comments

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.