Andy Thompson
Andy Thompson

Reputation: 304

Print multiple reg expr matches in same string in Perl

I have a file in the format of:

20120807 175041.438  5976.022 E   27000 [PRE:4712345678: Just some text HERE '127.0.0.1' or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438  5976.022 E   27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]
..

What I want to extract is:

20120807;4712345678;127.0.0.1;;
20120807;4722345679;127.0.0.2;26002;

I know I can extract IPs using eg. /(\d+\.){3}\d+/ and 10 digits starting with 4 using eg. /[4][0-9]{9}/ but how to print them together from same string?

Upvotes: 2

Views: 119

Answers (2)

Pavel Vlasov
Pavel Vlasov

Reputation: 3465

use strict;

while(my $line = <DATA>) {

    $line =~ m{
            ^
            (\d+)              # first number
            .*?
            (\d{10})           # 10 digits number
            .*? 
            ((?:\d+\.){3}\d+)  # ip
            .*?
            APU_ID\s' 
            (\d*)              # apu number
            '
    }x;

    printf "%s %s %s %s\n", $1, $2, $3, $4;
}

__DATA__
20120807 175041.438  5976.022 E   27000 [PRE:4712345678: Just some text HERE '127.0.0.1'     or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438  5976.022 E   27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or  APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]

Upvotes: 0

cdtits
cdtits

Reputation: 1128

while (<DATA>) {
    @ds = /^(\d+).*?PRE:(\d+):[^']+'([^']+)' or APU_ID '(\d*)'/;
    print "$_;" for @ds;
    print "\n";
}

__DATA__
20120807 175041.438 5976.022 E 27000 [PRE:4712345678: Just some text HERE '127.0.0.1' or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438 5976.022 E 27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]

output:

20120807;4712345678;127.0.0.1;;
20120807;4722345679;127.0.0.2;26002;

Upvotes: 1

Related Questions