LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-16-2003, 01:43 AM   #1
david_wliu
LQ Newbie
 
Registered: Mar 2003
Posts: 21

Rep: Reputation: 15
Question ^M character


After copying text files from Windows to Linux/Unix, there is always a ^M character at the end of each line when the file is viewed/edited on Linux. Is there any effective way to remove the ^M characters by scripts?

Thanks.
 
Old 03-16-2003, 02:01 AM   #2
doublefailure
Member
 
Registered: Mar 2002
Location: ma
Distribution: slackware
Posts: 747

Rep: Reputation: 30
i think sed s/\r\n/\n/g will work .. i've seen similar posting few days ago.. search with 'php sed'
 
Old 03-16-2003, 09:02 AM   #3
david_wliu
LQ Newbie
 
Registered: Mar 2003
Posts: 21

Original Poster
Rep: Reputation: 15
I will try your solution to see how it works. Thanks, doublefailure.

P.S. I did search with 'php sed', but didn't find any similar posting regading this issue. Could you send me the links if you see any?
 
Old 03-16-2003, 01:46 PM   #4
doublefailure
Member
 
Registered: Mar 2002
Location: ma
Distribution: slackware
Posts: 747

Rep: Reputation: 30
search with 'teaching php'
there's one
 
Old 03-16-2003, 02:03 PM   #5
doublefailure
Member
 
Registered: Mar 2002
Location: ma
Distribution: slackware
Posts: 747

Rep: Reputation: 30
also if u use emacs/xemacs
put this in your init file
and emacs will prompt u if it finds ^M in a file u open

(add-hook 'find-file-hooks 'remove-or-convert-trailing-ctl-M)
(defun remove-or-convert-trailing-ctl-M ()
"Propose to remove or convert trailing ^M from a file."
(interactive)
(save-excursion
(goto-char (point-min))
(if (search-forward "\^M" nil t)

(if (or (= (preceding-char) ?\^J)
(= (following-char) ?\^J) )

(if
(y-or-n-p "Remove trailing ^M ? ")
(progn (goto-char (point-min))
(perform-replace "\^M" "" nil nil nil)
(pop-mark) )
(message "No transformation.") )
(if (y-or-n-p "Convert ^M into ^J ? ")
(progn (goto-char (point-min))
(perform-replace "\^M" "\^J" nil nil nil)
(pop-mark) )
(message "No transformation.") ) )

)
))
 
Old 03-16-2003, 02:29 PM   #6
david_wliu
LQ Newbie
 
Registered: Mar 2003
Posts: 21

Original Poster
Rep: Reputation: 15
That's very helpful. thanks again, df
 
Old 03-16-2003, 11:16 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Another old trick to remove the unneeded carriage returns was/is:

cat old-dos-file.txt | col > converted-file.txt

Can't have too many tricks in your bag.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C character appending Chrax Programming 6 12-23-2004 11:32 AM
Character replacement SeT Linux - General 1 11-18-2004 12:21 PM
structure with character iftiuk Programming 4 06-03-2004 10:09 AM
strange character ^M chandan Linux - Newbie 2 07-09-2003 02:38 AM
i need help with a special character Hone101 Linux - Newbie 1 05-03-2003 07:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:11 PM.

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