Hi,

I am facing issues in parsing using Regex. The problem definition is as
below : -

A file with data :-

BEGIN
country Japan
passcode 1123
listname sales
contact ch...@example.com
contact m...@example.com
END

BEGIN
country Namibia
passcode 9801
listname dept
contact l...@example.com
END

BEGINDL
country US
passcode 4123
listname Investment
member a...@example.com
member b...@example.com
ENDDL

BEGIN
country US
passcode 4432
listname testing
contact lore...@test.com
contact a...@test.com
END
......
.....
...
..
.

I want to parse it in such a way that  all data with BEGIN and END goes in
one file and BEGINDL and ENDDL goes in other with kind of processing I want
to so.

I am using below code but doesnot work : -

#!/usr/bin/perl
my $file=shift;
open( FH , "$file" ) or die("open failed: $!\n");
open ($fh1, ">/tmp/a");
open ($fh2, ">/tmp/b");
my $check=0;
while (<FH>) {
#    next unless /BEGIN/ .. /END/ || /BEGINDL/ .. /ENDDL/ || eof;
    if($_ =~ /BEGIN$/ || ($check == 0) ) {
        print $fh1 $_;
        $check = 0;
        if($_ =~ /END$/) {
        $check = 2;
        }
    }elsif($_ =~ /BEGINDL/ || ($check == 1)) {
        print $fh2 $_;
        $check = 1;
        if($_ =~ /ENDDL/) {
        $check = 2;
        }
    }
    next unless($check == 2);
}

Any better suggestion ?

Regards.

Reply via email to