#!/usr/bin/perl
use strict;
use warnings;
my $retain = "";
while ()
{
chomp;
if (m{^()(.*)(
)$})
{
my ($begin, $middle, $end) = ($1, $2, $3);
# This line is inside a ...
match.
# Handle differently if it's the first line.
if (! $retain)
{
# Found first in block, replace with .
# Print beginning and middle, but retain end, in case
# this one line is also the end of the block.
print ""."$middle";
$retain = $end;
}
else
{
print "$retain\n"; # Finish off previous line
print "$begin"."$middle"; # Start new line
$retain = $end; # retain ending
}
}
else
{
# This line is not a ...
match,
# so flush preserved output, if any,
# and copy this line to the output.
if ($retain)
{
print "\n"; # Finish off previous line
$retain = "";
}
print "$_\n";
}
}
if ($retain)
{
print "\n"; # Finish off previous line
}
#
This is para2
#This is para3
__DATA__
This is para1
####
This is para1
####
This is para1
####
This is para1
This is para2
This is para3
####
This is para1
This is para2
This is para3
####
This is para1
This is para2
####
This is para1
This is para2
####
This is para1
This is para2
This is para3
####
This is para1
This is para2
This is para3