Remove ^M from file in unix file

This note is dedicated to my chubby hubby that keep asking me how to remove ^M in a file.

UNIX treats the end of line differently than other operating systems. Normally CTRL-M character is visibly displayed at the end of each line as ^M if the file is not transfered in binary mode (this is why I always prefer SFTP instead of FTP).

  1. In vi, to remove the ^M do as following :
  2. :%s/^V^M//g

    ^V is a CONTROL-V character
    ^M is a CONTROL-M.

    So it will look like this:

    :%s/^M//g

    %s is a basic search and replace command in vi.
    The g is use for search and replace globally (all occurrences).

  3. with Solaris you can just do (heard HPUX has the command “dos2ux” instead of dos2unix):
  4. dos2unix <dos file> <unix file>

  5. You can also use the octal representatiion of ^M:
  6. tr -d ‘\015\032′ < file

2 Responses to “Remove ^M from file in unix file”

  1. azlan Says:

    tq honey…very helpfull

    [Reply]

  2. fahitah Says:

    Terima kasih atas ingatan.

    [Reply]

Leave a Reply