How 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 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.

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

If you are not interested doing that using regex there is a even simpler methos using dos2unix

apt-get install dos2unix

 

$dos2unix filename

Leave a Reply

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