Saturday, April 2, 2011

How to remove CONTROL-M characters in UNIX or Linux

Control-m characters will get appended to a file when a file is transferred from windows to UNIX machine. There are multiple ways it can be removed.

Using vi editor:

Using col command:
cat filename | col -b > newfilename

Using sed command:
sed 's/^M//g' filename > newfilename

Using dos2unix comand:
dos2unix filename newfilename


volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk
volcano@volcano-laptop:~/shellscript/ctrl$ cat jsk | col -b >
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v
volcano@volcano-laptop:~/shellscript/ctrl$ sed 's/^M//g' jsk > jsk.new2
volcano@volcano-laptop:~/shellscript/ctrl$ cat -v jsk.new2

Note: Hold the control key and then press v and m to get the control-m character


ujjwal singh said...

Thanks Sujay,

Narasimha Rao said... provides CONTROL-m in hyderabad with best faculties on real time projects. We give the best online trainingamong the CONTROL-m in Hyderabad.
Classroom Training in Hyderabad India

venkatesh reddy said...

using any of the above command(sed,dos2unix,etc) i am not able clear CONTROL-M characters. Please suggest us to solve this issue.

Murali said...

Great job! thanks a million !!
I used the following command from the above and it worked like a champ. I am on AIX 6.1

cat filename | col -b > newfilename

Thanks a lot!!!!

mahasiswa teladan said...

hi...Im student from Informatics engineering nice article,
thanks for sharing :)

rahim said...

Very goo, Thanks :)

James Sa said...

why dos2unix doesn't remove ^M if the dos text file has ^M without line feed?

Seshank satavilli said...

I am not able to remove the ^M characters.

the contents of the file:


How to remove these?

Desired content in the file

Thanks in advance.

Bogon said...

For the inline ^M, you can use this:

$ sed 's/\cM/\n/g' file.txt > newfile.txt

This assumes that your text stream ends with a newline (conventionally \n)

murali krishna said...

Command suggested through vim doesnt work. Instead try this

Sri said...

I have tried Col option which really helped me. Thanks much

joshnson haddin said...

A guarantee of appreciation is oracle training in chennai all together for sharing the key data and its really stunning.informatica training in chennai


good explanation has been given about Linux.This is the best article i have seen
about Linux.thanks for giving good explanation.
oracle fusion procurement online training

Calfre India said...

Oracle fusion financials online training institute we have our branch over all the india. Oracle Fusion Financials Online Training in Hyderabad, Bangalore, Delhi, Chennai, Kolkata, Pune, Mumbai, Ahmedabad, Gurgon, Noida, India, Dubai, UAE, USA, Kuwait, UK, Singapore, Saudi Arabia, Canada, Oracle Fusion financials Online Trainings

Oracle fusion Financials Online Training

Oracle Fusion Financials online Training

Priya Kannan said...

I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.
UNIX Training in Chennai

oracle34 said...

thank for sharing use full information for more
Attend free demo classes at igofusion for Oracle Fusion Financials coaching