Removing Carriage Returns (^M) from Files

If you have a file under Unix and you are seeing a lot of ^M characters at the end of each line, it's probably a file that's been created on a DOS/Windows system. By default DOS/Windows terminates each line with a carriage return/new-line pair. I believe that this comes from the days when you had to send these characters to the printer in order to move the print head to the beginning of the line, and then move it to the next line using seperate control sequences.

The easiest way to remove these characters is to type:


cat file.txt | tr -d '\
' > file_new.txt

Replace "file.txt" with the name of the file, and "file_new.txt" with the name of the new file which has the carriage returns removed. Don't make these the same name!