G'day Ganesh Bharadwaj1,
Below is my take on a solution.
I did some additional testing as follows (all successful):
-
I originally used your data and got your expected output.
-
I then added "test 1" and got your expected output again.
-
I then added "test 2a" and "test 2b": your expected output again.
-
I then added "test 3": your expected output again.
-
I then added "test 4": your expected output PLUS the RRRRR line at the end.
#!/usr/bin/env perl
use strict;
use warnings;
my ($skip, @buf);
while (<DATA>) {
if ($skip) {
$skip = 0;
next;
}
if (/XXXXX/) {
@buf = ();
$skip = 1;
}
else {
push @buf, $_;
$skip = 0;
}
if ($. > 1 && @buf >= 2) {
print for @buf[0 .. $#buf - 1];
@buf = ($buf[-1]);
}
}
print for @buf;
__DATA__
XXXXX - additional test 1
PPPPP
XXXXX is my name
YYYYY
KKKKK
UUUUU
BBBBB
CCCCCC
XXXXX is what I play
KKKKK
NNNNN
ZZZZZ - additional test 2b
XXXXX - additional test 2a
QQQQQ - additional test 3
RRRRR - additional test 4