• Sponsored Links

Removal of control M Characters from a file in Unix

Share this :

Removal of control M Characters from a file in Unix


Control M Characters means ^M Characters Control-M is a character found at the end of a line usually in files transferred from windows operating system to unix operating system

^M is a carriage return.

You should understand a few things first:

CR = \r = Carriage Return
LF = \n = Line Feed

In DOS, all lines end with a CR/LF combination or \r\n.
In UNIX, all lines end with a single LF or \n.

The ^M that you are seeing is actually a CR or \r. If you want to test for carraige returns in a file, you want to look for \r. Try this on the file:

There are many ways to remove Control M characters :


Fix it at the cause

when transferring files from windows make sure the EOL is in unix format . You can do that by opening file in notepad++ –> Edit –> EOL Conversion –> Unix/OSX Format

Now if you transfer these files to Unix Server , there wont be any Control M characters .


Fix when file is moved already to Unix server

Way 1 : Check if there are any control M characters :

Command : grep -r $’\r’ filename

Use -r for recursive search and $” for c-style escape in Bash

Way 2 : if you are sure it’s text file, then it should be safe to run below command to remove all \r in a file.

Command : tr -d $’\r’ < filename

Way 3: If use GNU sed, -i can perform in-place edit, so you won’t need to write back:

Command : sed $’s/\r//’ -i filename

Way 4 : dos2unix command . Below command will convert FileWithControM.txt. and put cleaned data in CleanFile.txt

Command : dos2unix FileWithControM.txt

Man page :http://linuxcommand.org/man_pages/dos2unix1.html

Way 5 : Remove control M while in vi

Command: vi filename
In Escape Mode :%/s/^M//g

Note : To get ^M do a Control V + M i.e. keep pressing Control Key ,first press V and then M



References :




Comments are closed