my $wait_for_odd_quotes = 0; my $line_accumulator = ''; while(<$CSVFILE>) { chomp(my $this_line = $_); my @matches = $this_line =~ /(\")/g; my $count = @matches; if($wait_for_odd_quotes == 0){ if($count % 2 == 1){ $line_accumulator = $this_line; #Reset Accumulator $wait_for_odd_quotes = 1; #Prime next loop to look for end of quotes } else { print $OUTFILE $this_line . "\n"; #We are not looking for and end quote and this line doesn't have an odd number of quotes so we'll write it to file } } else { if($count % 2 == 1){ $line_accumulator .= $this_line; #matched our open quotes, taking this last bit $wait_for_odd_quotes = 0; #reset so next loop knows we're not looking to close print $OUTFILE $line_accumulator . "\n"; } else { $line_accumulator .= ' ' . $this_line; } } } print $OUTFILE $line_accumulator . "\n"; #catch final line if it had embedded newlines