Operating System - HP-UX
1833875 Members
3291 Online
110063 Solutions
New Discussion

Removing carriage return control characters from an ASCII file

 
SOLVED
Go to solution
Paul Ammirata
Occasional Contributor

Removing carriage return control characters from an ASCII file

Hi,

I have an ASCII file with carriage return control characters. When I browse the file in vi it looks like this:

line 1 of text^M
line 2 of text^M

How can I remove the carriage return control characters so that I'm left with:

line 1 of text
line 2 of text

Thanks,
Paul
6 REPLIES 6
Sachin Patel
Honored Contributor
Solution

Re: Removing carriage return control characters from an ASCII file

Hi Paul,
#dos2ux file1 > file2
#mv file2 file1

Now your file1 will be without ^M

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: Removing carriage return control characters from an ASCII file

Hi Paul:

You can easily deal with this with the 'dos2ux' (and inversely, with the 'ux2dos') utilities. See the man pages for more information:

# dos2ux oldfile > newfile

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Removing carriage return control characters from an ASCII file

Hi Paul:

Another variation on the same theme:

cat oldfile | tr -d "\015" > newfile

tr -d will delete the CR's (octal 15's)
If it ain't broke, I can fix that.
Uday_S_Ankolekar
Honored Contributor

Re: Removing carriage return control characters from an ASCII file

Hello

Try this
sed 's/^M//' FILENAME > NEWFILE
or
use
# dos2ux /tmp/myfile > /tmp/myfile.new

Good luck
-USA..
Good Luck..
harry d brown jr
Honored Contributor

Re: Removing carriage return control characters from an ASCII file

If you are already into the file using vi and you see them, then do this:

:1
:s/ctrl-Vctrl-M$//999999

note ctrl-V and ctrl-M will redraw into ^M - don't forget the $ sign
Live Free or Die
Darrell Allen
Honored Contributor

Re: Removing carriage return control characters from an ASCII file

Hi Paul,

As you can see there IS more than one way to skin a cat. If you got the file via FTP from a Windows environment using binary mode you'd get the ^M. Use ascii mode and FTP takes care of the translation for you. Otherwise, dos2ux is great.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)