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.

12 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?

RSS feed for comments on this post · TrackBack URI

Leave a Comment