my $string = q{ this is a sample text. all single blank lines will be removed. all double, triple, or more blank lines will not be touched. }; $string =~ s#\n(\n+)#"\n" x (length($1)>1 ? length($1)+1 : length($1))#eg; print $string; __END__ this is a sample text. all single blank lines will be removed. all double, triple, or more blank lines will not be touched.