LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to remove the "^M" in some dos file (https://www.linuxquestions.org/questions/programming-9/how-to-remove-the-%5Em-in-some-dos-file-474608/)

naihe2010 08-16-2006 10:04 PM

How to remove the "^M" in some dos file
 
When I edit some dos file int Vim, it has a "^M" at the end of each line.

How to remove them from my file?

rickh 08-16-2006 10:16 PM

You need a utility named something like "dos2unix" Different distros give it different names, but it reformats files createdin DOS, to a unix format.

AzrielMacKay 08-16-2006 10:18 PM

I think you can run fromdos on it and remove them.

soggycornflake 08-17-2006 11:17 AM

If you can't find dos2unix (I haven't got it on Slackware), you can use

Code:

perl -i -wpe 's/\r$//' <files>

Spudley 08-18-2006 08:09 AM

^M is the carriage return character.

Windows text files use a combination of carriage return plus line-feed characters as the end-of-line marker; Unix, Linux, and most other platforms use just the line-feed character. Files created on Windows will therefore have additional ^M characters at the end of every line when viewed in Linux. Likewise, files created in Linux and viewed in Windows will lose all their line-breaks.

Solutions:

1) As has been stated, there are utilities to convert files between the two formats.

2) If you're using FTP to transfer the files, switch it to ASCII mode when you upload or download the files, and the FTP server will convert the line feeds automatically for you.

3) Open the file in an editor that can recognise either format -- in Windows, use Wordpad instead of Notepad. In Linux, I find that KDE's KWrite does the job very nicely, but I'm sure there are others that work just as well.

Hope that helps :)

ppanyam 08-21-2006 12:15 AM

Code:

tr -d '\r' < inputfile > outputfile
also should work.

ppanyam

naihe2010 09-08-2006 03:22 AM

Quote:

Originally Posted by ppanyam
Code:

tr -d '\r' < inputfile > outputfile
also should work.

ppanyam


Good method, thank you.

And I defined
Code:

map <C-d><C-u> :%s/&\r$//g^M
in my .vimrc.

When I edit a dos file with vim, I can type Ctrl+d,Ctrl+u remove them.

druuna 09-08-2006 04:36 AM

Hi,

Another possibility (in vim):

set ff=unix / set ff=dos

lorebett 09-08-2006 10:39 AM

you can install recode (if not already installed) and run

recode dos 'yourfile'

uselpa 09-09-2006 04:08 PM

Quote:

Originally Posted by soggycornflake
If you can't find dos2unix (I haven't got it on Slackware)

Slackware has fromdos.


All times are GMT -5. The time now is 04:52 PM.