Operating System - HP-UX
1824976 Members
3210 Online
109678 Solutions
New Discussion юеВ

How to delete ^M characters from a file ?

 
SOLVED
Go to solution
Preet Dhillon
Advisor

How to delete ^M characters from a file ?

Dear Colleagues,

I have a text file with each line terminated by a ^M character. I can see these at the end of each line if I open the file in vi. I've tried sed, tr and global search and replace in vi but the ^M still appears when I view the file. Is there a way of removing this control character?
Many thanks and regards,
Preet
Nothing succeeds like excess
8 REPLIES 8
Dietmar Konermann
Honored Contributor
Solution

Re: How to delete ^M characters from a file ?

Preet,

$ whence dos2ux
/usr/bin/dos2ux

dos2ux outfile

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
H.Merijn Brand (procura
Honored Contributor

Re: How to delete ^M characters from a file ?

If there are *only* ^M's (and no LF's)

# perl -pe 's/\r/\n/g' infile > outfile

If there is (M$ like) CRLF line endings

# perl -pe 's/\r$//' infile > outfile

If it is (Mac like) LFCR line endings

# perl -pe 's/^\r//g' infile > outfile


HTH
Enjoy, Have FUN! H.Merijn
Jean-Luc Oudart
Honored Contributor

Re: How to delete ^M characters from a file ?

probably ftp as nim instead of ascii file ?

anyway you can the following filter :
cat | tr -d "\r" >

simple.

Jean-Luc
fiat lux
Trond Haugen
Honored Contributor

Re: How to delete ^M characters from a file ?

dos2ux can tak care of that: 'dos2ux file >file.ux'.
Within vi you can do it with ':g/^M/s///'.
The '^M' is one character and is composed by 'Ctrl-v Ctrl-m'.

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Bill McNAMARA_1
Honored Contributor

Re: How to delete ^M characters from a file ?

similarly, before transfering text files to winders, you might want to ux2dos

Later,
Bill
It works for me (tm)
Sean OB_1
Honored Contributor

Re: How to delete ^M characters from a file ?

Preet,

dos2ux is the way to go. It's a little utility made just for this.

dos2ux fixedfile

Typically this is caused by uploading a text file as binary instead of ascii.

Although I've seen some systems where unzip'ing a zip file containing text files ends up with the same problems.
Kyoungjin Kim_1
Occasional Advisor

Re: How to delete ^M characters from a file ?

Hello,


Please try
# cat infile | col -b > outfile

This can be helpful when you'd like to save man pages into an ascii file.

Hope this helps.
Mynor Diaz
New Member

Re: How to delete ^M characters from a file ?

Preet,

Here is the anwer to your question. I encountered the same problem with a program I was sent a few months ago. The origin of the problem was the way the programmers compiled the code, so when the files were sent to me in a zip file, it did not matter how I sent (ftp) them to the Unix server I am using, they all had the metacharacter. You can find the solution here:

http://icarus.weber.edu/home/bob/cs213/rm_ctr_m.html

You may also want to look at this forum discussion:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xae5f7d4cf554d611abdb0090277a778c,00.html
It's just a thing!