How to remove ^M from unix files using VI editor

^M is DOS line break charater which shows up in unix files when uploaded from a windows file system in ascii format.

To remove this, open your file in vi editor and type

:%s/(ctrl-v)(ctrl-m)//g

and press Enter key.

Important!! – press (Ctrl-v) (Ctrl-m) combination to enter ^M character, dont use “^” and M.

If anything goes wrong exit with q!.

I learned this from here.

29 Comments »

  1. L(C^2) said

    Very good!

    I just encountered this problem and it is so annoying to replace each ^M one by one!

    Thank the god here is the solution~
    :-)

  2. rachel said

    i love ram\’s!

  3. How can I reverse all the text in a file using vi editor command?

  4. bob said

    freakin awsome. thank a bunch.

  5. Scott said

    Your substitution command may catch more ^M then necessary. Your file may contain valid ^M in the middle of a line of code for example. Use the following command instead to remove only those at the very end of lines:
    :%s/(ctrl-v)(ctrl-m)*$//g

    In some flavors of vi (e.g. RedHat), vi does it for you with:
    :set fileformat=unix

  6. j habib said

    How to add ^M (Ctrl+V+M) in one particular line in an input file using script? Is it possible to store ^M in a variable?

  7. aymeric said

    the simplest way is to use the command dos2unix

  8. swathi said

    how can i remove 200c in vi editor?

  9. Avnit said

    how to do this in multiple file , is there a way we can make a program for this

  10. srini said

    Thanks a lot.. Uploaded a file from excel on to sever box with bunch of ^M
    … This post help me…

    Thanks
    srini

  11. pavan said

    Thank you. It helped me

  12. sab said

    Thanks guys…this worked for me to remove ^M from my input file. But any idea why this comes when we ftp the a file from windows to UNIX box on ASCII mode?

  13. Sreenadh said

    Thanks guys… It helped me. :)

  14. Jp said

    Thanks

  15. Brajendra said

    I was really pissed off with this ^M in my file,
    thanks to Scott i use
    :set fileformat=unix
    and it works……

  16. zalgo said

    @Scott: thanks the “:set fileformat=unix” worked for me on Ubuntu 10.04

  17. anant said

    thanx alot ..////

  18. pcderic said

    This guidance didn’t help me.

    However, using “:set fileformat=unix” did.

  19. Ehsan said

    Thanks!

  20. Sameer said

    Thanks a lot….

  21. SysAdmin said

    just use dos2unix that’ll do the work

  22. Narendra. said

    Thanks.

  23. J-Y Zhang said

    Thanks a lot!

    very helpful.

  24. Umesh said

    Use dos2unix command. i.e. to replace all DOS characters to Unix -
    $ dos2unix file.c file.c

  25. Indy said

    ^M comes from MAC files.

  26. kgauri said

    Thanks a lot…!!

  27. Spooled file from Oracle SQL contained ^M at the end of every line along with a carriage return. This tip removed them, which enhanced readability tremendously. Thanks!!!

  28. [...] vinda daqui , que estava perdida a pelo menos seis meses na tralhas [...]

  29. Yefb said

    Reblogged this on Infornética.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.