Reject the record if the record in the next line does not satisfy the pattern

Hi,
I have a input file with the following entries:
1one
2two
3three
1four
2five
3six
1seven
1eight
1nine
2ten

The output should be
1one
2two
3three
1four
2five
3six
1nine
2ten

The record that begins with 1 should have the next record to begin with 2 or 3 else reject that record.
The rejected records should be captured in a file and the valid records in another file.

Hi supchand,

Try:

$ cat infile
1one
2two
3three
1four
2five
3six
1seven
1eight
1nine
2ten
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 input-file valid-file rejected-file\n] unless @ARGV == 3;

open my $reject_fh, ">", pop @ARGV or die qq[Cannot open file for writing: $!\n];
open my $valid_fh, ">", pop @ARGV or die qq[Cannot open file for writing: $!\n];

my ( @serie );

while ( <> ) {
        chomp;
        if ( /\A1/ ) {
                push @serie, $_;
                next;
        }

        if ( @serie <= 1 ) {
                printf $valid_fh qq[%s%s\n],
                        @serie ? $serie[0] . qq[\n] : qq[],
                        $_;
        } else {
                printf $valid_fh qq[%s\n%s\n], pop @serie, $_;
                printf $reject_fh qq[%s\n],
                        join( qq[\n], @serie );
        }

        @serie = ();
}

END {
        if ( @serie ) {
                printf $reject_fh qq[%s\n],
                        join( qq[\n], @serie );
        }
}
$ perl script.pl
Usage: perl script.pl input-file valid-file rejected-file
$ perl script.pl infile valid.txt rejected.txt
$ cat valid.txt 
1one
2two
3three
1four
2five
3six
1nine
2ten
$ cat rejected.txt 
1seven
1eight

Regards,
Birei

Hi,
Thanks for your response. Can you please help me out this query in unix scripting.