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:
:%s/^M//g

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

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

Using dos2unix comand:
dos2unix filename newfilename

Examples:

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

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

18 comments:

ujjwal singh said...

Thanks Sujay,

Narasimha Rao said...


Ecorptrainings.com 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:

text1^Mtext2^Mtext3^M

How to remove these?

Desired content in the file
text1
text2
text3

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
:%s/\^\M//g

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

ORACLE FUSION PROCUREMENT said...

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


suresh H said...

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.
Best AngularJS Training in Chennai
Hadoop Training in Chennai
Big Data Training in Chennai

Sandeep Oracle said...

That is a good tip, especially to those new to the blogs here but very accurate information. Very nice blog post!! Thanks for the posting that is a really neat written blog..........Visit our website known about Fusion SCM Training in Ameerpet Hyderabad.