On Fri, Feb 27, 2009 at 23:12,  <mysticp...@yahoo.com> wrote:
> I have a input file ,which has begin ... end patterns
> some of the records are not delimitted end missing,
>
> How do I separate records that dont have BEGIN and END ,
> and  collect informtion of what ever date ,thats  in the record
> two lines of data for each record,could be more lines
>
> Write perl grammer tokenise the records ?
>
>
> open(IN,"<iostat.txt");
>
> while(<IN>){
> if(/^BEGIN/ .. /^END/){
> ...
> ...collect the record
> ....
> }
> }
snip


#!/usr/bin/perl

use strict;
use warnings;

$/ = "END\n";
while (<DATA>) {
        chomp; #remove END tag

        #we can only detect missing BEGIN tags after an END tag
        #this also gets rid of the first BEGIN tag, so the split
        #that follows will work better
        print "no BEGIN tag on record $.\n" unless s/\ABEGIN\n//;

        #If this split produces more than one record
        #then we have missing END tags
        my ($record, @extra_records) = split /^BEGIN\n/m;

        print "no END tag on record $.\n" if @extra_records;
        print "$record";

        while (defined(my $record = shift @extra_records)) {
                $.++; #we need to tell Perl we found another record
                print "no END tag on record $.\n" if @extra_records;
                print "$record";
        }
}

__DATA__
BEGIN
rec1 good
END
BEGIN
rec2 missing end
BEGIN
rec3 good
END
BEGIN
rec4  good
END
BEGIN
rec5 good
END
rec6 missing begin tag
END
BEGIN
rec7 missing end tag (undetectable)
rec8 missing begin tag (undetectable)
END


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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