Remove ^M characters in vi editor at end of lines

Sometimes on editing files in linux or windows, ^M (Control M) character appears at the end of lines in vi editor.

You can use sed command to remove the CTRL M characters.

$ sed -e “s/^M//” file > filenew
To enter ^M, type CTRL-V, then CTRL-M (use the CTRL keys).

You can do this in vi also.

Inside vi editor, in ESC mode, type the following

:%s/^M//g

Press Enter and you are done.