LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2006, 10:04 PM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
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?
 
Old 08-16-2006, 10:16 PM   #2
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 62
You need a utility named something like "dos2unix" Different distros give it different names, but it reformats files createdin DOS, to a unix format.
 
Old 08-16-2006, 10:18 PM   #3
AzrielMacKay
Member
 
Registered: Jul 2001
Location: Moody, AL
Distribution: Debian and Kubuntu
Posts: 249

Rep: Reputation: 30
I think you can run fromdos on it and remove them.
 
Old 08-17-2006, 11:17 AM   #4
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

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

Code:
perl -i -wpe 's/\r$//' <files>
 
Old 08-18-2006, 08:09 AM   #5
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
^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
 
Old 08-21-2006, 12:15 AM   #6
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
Code:
tr -d '\r' < inputfile > outputfile
also should work.

ppanyam
 
Old 09-08-2006, 03:22 AM   #7
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
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.

Last edited by naihe2010; 09-08-2006 at 03:25 AM.
 
Old 09-08-2006, 04:36 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Another possibility (in vim):

set ff=unix / set ff=dos
 
Old 09-08-2006, 10:39 AM   #9
lorebett
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Gentoo
Posts: 57

Rep: Reputation: 16
you can install recode (if not already installed) and run

recode dos 'yourfile'
 
Old 09-09-2006, 04:08 PM   #10
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Quote:
Originally Posted by soggycornflake
If you can't find dos2unix (I haven't got it on Slackware)
Slackware has fromdos.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove "lastlog" log file shipon_97 Linux - Security 1 07-19-2006 04:29 AM
Cedega from Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Software 0 03-30-2005 11:24 AM
Cedega and Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Games 0 03-26-2005 02:48 AM
remove "-" (dash) from file aurelio26 Linux - Newbie 4 09-16-2004 11:32 AM
remove "special" text out of a file tearinox Linux - General 1 03-15-2004 08:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration