use strict; use warnings; undef $/; my $str; ($str=)=~s{(((\n|^)K (.*?)(?=\n|$))+)}{"$3

".${($_=$1)=~s!(\n|^)K !,%!g,\$_}."

"}eg; $str=~s/(

),/$1/g; print $str;