Formatting Substitution Command Not Working in vi

Unix Gurus,

I have a text file I have FTP'd to UNIX so that I can use it to load into our Baan system. When vi the file, I can see that there are formatting characters at the end of each line (^M). To get rid of these, I have read that the following should work:

:%s/^M$//g - with the ^M being the equivalent of CTRL V CTRL M

Unforutnately, when trying to type this command I can only get to the "CTRL M" before I get the error message "Substitute pattern match failed." Does anyone know why I cannot type the whole command shown above or is there something that cause the command to execute before the entire command is typed?

Thank you in advance for your feedback.

Leave vi. The simplest solution is to do a dos2unix on the file that you just ftp'ed.

tranfer the file using ascii mode instead of binary, the carriage returns should be removed automatically.

Am surprised, why that happens!

Definitely you would be able to do that!

I tried and it works ( I doubt the ctrl -v and ctrl - m )

Thanks Gurus! I never heard of or used DOS2UNIX so maybe I will try it sometime. I did take a look at my FTP settings and it was set on "Automatic." After changing it to "ASCII," I FTPd some files over that I had problems with in the past. Sure enough, no more problems with character formatting! I guess I assumed that "Automatic" would always give me the best method of moving the file.

I still would also be interested in knowing why I am not able to use the command %s/^M//g. The commands I am using to get the formatting character are ctrl v followed immediately by ctrl m. There is no use of the shift key, minus signs, plus signs, etc - only the ctrl button with the letter v follwed by the ctrl button and the letter m.

In VI the following works ,
:%s/^M//g

OR

Give the below ones a try,
tr -d \015 \032 < inputFile
tr -d '^M' < inputFile
sed 's/^M//g' < inputFile

Thanks
Nagarajan Ganesan

Nagarajan,
Your process is lengthy. I simply go for following
echo "foo" >> file-name
Then edit file using "vi" editor. your "^M" will be visible. Just remove that using compatible "vi" pattern-replacement method. Remember last-line "foo" and save file.

Special attention is required in above make sure you use ">>" and NOT ">" otherwise its gone.

hey i never tried this in vi but works for vim
just ^M character is carriage return ( which indicates file is in DOS format ) so just open file and do
:set fileformat=unix after that save file .

So, the error msg denotes that your ctrl-v or ctrl-m is causing the "Enter" and since the pattern substitution is not complete thats why the error.

Is is possible that either ctrl-v or mostly ctrl-m is mapped to the "enter" or some key ?

Give us the output of "stty -a" and possible the "map" values in your .exrc file.

^M is often enter, but it shouldn't make any difference. Once ^v is typed, the next char should be treated literally.

Agreed. Hmm .. can't think of anything else.

To get ridge of ^M from dos first rename dos file to *.dos, then, use col -b

cat *.dos | col -b > original file name