$/=undef; # Slurps all data in file. $_=; # Put it in $_, convenient for regex. my $lines=0; # Line count $. might suffice and then the pre increment can be dropped. my $records=11; # Number of lines in a record. s/\n/(',',"\n")[++$line%$records==0]/eg;# replaces all \n with commas except the recordth. print $_;