How to remove ^M characters from a file in Linux

Quick way to remove ^M characters from a file in Linux.

If you are opening a file in Linux which was previously edited in windows you get a ^M character at the end of each line. This is actually the CR and LF (Carriage Return and Line Feed) created by editors. It is not easy to read those files and you need to remove them. Easiest way to remove then is using a regular expression using sed.

Code:

$ sed -i "s/\^M//" filename

If you are not interested doing that using regex there is an even simpler method using dos2unix .

apt-get install dos2unix
$dos2unix filename

Leave a Reply

Your email address will not be published. Required fields are marked *