How to remove Ctrl-M character from Text file

What is this Ctrl-M character? Why are those in the file? How to avoid and remove these? These ^m characters are a special character and present in a file because Windows and Unix have different line termination character.

Wondering, why I am writing this post……!!!

It is common problem faced during development. Mostly developer work on Windows platform, create Unix shell script file for FTP, SQL loader, etc and move that file to Unix operating system and define a concurrent program on it. When you submit the Unix host based concurrent program, it ends in error. DBAs complains that there is Ctrl-M character in the file.

Unix and Windows use different line termination characters. Unix use only line feed while windows use both carriage return and line feed as termination characters. This CR and LF become Ctrl-M in Unix.

line termination character windows and unix

Avoid Ctrl-M character

Let me tell you the way to avoid Ctrl ^M character from a file.

Text editor option

We use either Notepad++ or TextPad while working on any text file. These editors generally have built-in option to save the file in Unix or Windows format.

In Notepad++, navigate to Edit->EOL Conversion and chose the respective file format. Here you need to choose UNIX/OSX format.

notepad eol conversion

FTP file in ASCII Mode

We use sFTP or FTP software like WinSCP to transfer files from Windows to Unix environment. This software provides options to transfer file using either Binary or ASCII mode.

Binary Mode – This mode we use to transfer binary file like form builder file .fmb, report builder file .rdf. It just moves the file as is with all properties.

ASCII Mode – This mode we use to transfer text file. It moves the file from one system to another and it also does explicit character conversion. Windows format file is automatically converted to Unix format file.

Explicit character conversion

Some Unix operating system like Solaris provides built-in command/utility dos2unix and unix2dos to convert file from Unix to DOS and vice versa.

dos2unix dos_format.txt unix_format.txt
unix2dos unix_format.txt dos_format.txt


These are different methods to remove Cntrl-M character. I am suggesting to follow approach 1 combined with approach 2.

Want to know more about line termination character, just head on to Wikipedia.

Related Post