On Thu, Jan 03, 2013 at 03:53:20PM +0530, punit jain wrote:
> Hi,
> 
> I am facing issues in parsing using Regex. The problem definition is as
> below : -

> 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 : -

What doesn't work?  It seems fine to me.

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

You probably want $check = 2 here.

> 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 ?

Depends on how you define better, but perhaps

 $ perl -ne 'print if /BEGIN$/ .. /END$/' < file > /tmp/a
 $ perl -ne 'print if /BEGINDL$/ .. /ENDDL$/' < file > /tmp/b

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to