In Perl, how to remove ^M from a file?

Alex Wong picture Alex Wong · Mar 16, 2009 · Viewed 112k times · Source

I have a script that is appending new fields to an existing CSV, however ^M characters are appearing at the end of the old lines so the new fields end up on a new row instead of the same one. How do I remove ^M characters from a CSV file using Perl?

Answer

Can Berk Güder picture Can Berk Güder · Mar 16, 2009

^M is carriage return. You can do this:

$str =~ s/\r//g