How to remove Ctrl-M (^M) from Unix files?
Sometimes when you upload or FTP files from a Windows system to a UNIX box you will see ^M at the end of each line. ^M is the UNIX equivalent of DOS line break. Not only does it not look pretty, this extra character may break all sorts of scripts that you try to run on your file. So how do you remove ^M? Here are two ways to get rid or ^M (Ctrl-M) from your files.
Option 1.Use dos2unix utility. The easiest way is to use dos2unix utility.
dos2unix originalfile convertedfile.
You can run man dos2unix
to learn about all the options.
Option 2. If you do not have dos2unix utility the next best option is to edit the file in VI. Open the file in VI editor and do a global search and replace.
:%s/(ctrl+V)(ctrl+M)//g
and hit enter.
Be aware that ^M is not just a ^ followed by a M. You will have to press CTRL and V to get ^ and then CRTL and M to get the M.
After replacing ^M do :wq or ZZ to save and exit the VI editor.
Enjoy.
bill
June 23, 2009 @ 5:47 pm
You saved me a lot of headache. Thanks for the useful tip.