use strict; undef $/; my $infile = (<>); my @fields = $infile =~ /(".*?"|\n)/gms; my $place = "\n"; foreach (@fields) { if ($place ne "\n" && $_ ne "\n") {print ",";}; unless ($_ eq "\n") {s/\n//g;}; print "$_"; $place = $_; }