Operating System - HP-UX
1833589 Members
3751 Online
110061 Solutions
New Discussion

Control chars displaying while doing a vi

 
SOLVED
Go to solution
N. Hanson
Occasional Contributor

Control chars displaying while doing a vi

Whenever I transfer a file created in Windows Notepad to Unix and edit the file, a ^M appears at the end of each line(representing a carriage return). How do I get rid of those during my vi session?
5 REPLIES 5
James A. Donovan
Honored Contributor
Solution

Re: Control chars displaying while doing a vi

If you're using FTP to do the transfer of text files, then you should use use the ascii transfer mode not binary.

To get rid of those ^M characters run your file through the dos2ux utility

# dos2ux filename > newfilename

The reason you see the ^M is because of the difference in the way Windows handles the en-of-line character. In Windows, it is actually comprised of two characters a carriage return (CR) followed by a line feed (LF), whereas in Unix the end of line character is simply a LF.
Remember, wherever you go, there you are...
Bruce Regittko_1
Esteemed Contributor

Re: Control chars displaying while doing a vi

Hi,

...And if you forget to run dos2ux before executing vi, do this last line mode command:

:%s/^m//

It substitutes the ^m on each line with nothing. To get the ^m into vi, press the ctrl key and hold it down. Then press the v key, release, then the m key, release the m and then the ctrl key. This ^v trick will work for any control character you wish to embed into your document.

--Bruce
www.stratech.com/training
Steve Post
Trusted Contributor

Re: Control chars displaying while doing a vi

Bruce is right. I've been doing it that way for years. Another option is if you need to automate it a little. You could make a script that removes the control-M.

#!/bin/ksh
# myscript
cat $1 | tr -d "\015" > ${1}.no_em
mv ${1}.no_em ${1}
The \015 is octal for the control-M. tr is the transliterate command.
Steve.
Joseph C. Denman
Honored Contributor

Re: Control chars displaying while doing a vi

A better solution would be to ftp the file as ascii. The ^M are carriage returns. If you ftp the file as ascii, it will automatically convert the carriage return to a newline which is what unix uses.

Hope this helps...


...jcd...
If I had only read the instructions first??
Printaporn_1
Esteemed Contributor

Re: Control chars displaying while doing a vi

Hi,
Please use dos2ux command to convert dos file to unix file

#dos2ux dosfile > unixfile

Thanks
enjoy any little thing in my life