dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

How to replace ^M with a Hard Return with "sed"? - shell

This is a discussion on How to replace ^M with a Hard Return with "sed"? - shell ; Dear Everyone: I am using a "Mac OS X" machine and work in an X11 window. I got an ASCII file from someone, and when I "more" the file, the entire file shows up as one continuous stream interpersed with ...


Home > Database Forum > Operating Systems > shell > How to replace ^M with a Hard Return with "sed"?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-13-2008, 03:15 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default How to replace ^M with a Hard Return with "sed"?

Dear Everyone:

I am using a "Mac OS X" machine and work in an X11 window.

I got an ASCII file from someone, and when I "more" the file, the
entire file shows up as one continuous stream interpersed with ^M
(Control-M which is the line-separator). Now I can use the following
command

% sed '/s/^M//g' filename > newfilename

to get rid of the ^M signs, but the resulted new file is in one
continuous line. My intention, however, is to replace each ^M with a
hard return.

So my question: How do I represent the "hard return" in the above
"sed" command? (which needs to be put between the last two slashes in
the above command.)

Thank you for reading and replying!

--Roland
Reply With Quote
  #2  
Old 11-13-2008, 04:25 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

qquito ÐÉÛÅÔ:
> Dear Everyone:
>
> I am using a "Mac OS X" machine and work in an X11 window.
>
> I got an ASCII file from someone, and when I "more" the file, the
> entire file shows up as one continuous stream interpersed with ^M
> (Control-M which is the line-separator). Now I can use the following
> command
>
> % sed '/s/^M//g' filename > newfilename
>
> to get rid of the ^M signs, but the resulted new file is in one
> continuous line. My intention, however, is to replace each ^M with a
> hard return.
>
> So my question: How do I represent the "hard return" in the above
> "sed" command? (which needs to be put between the last two slashes in
> the above command.)
>
> Thank you for reading and replying!
>


col -bx < file > newfile
But I don't known that exist `col' on MacOS or not.


--
UV-RIPE
Reply With Quote
  #3  
Old 11-13-2008, 04:40 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

In article ,
Michael Vilain wrote:

> cat filename | tr "\r" "\n" > newfilename


BTW, you can do it without the cat and pipe like this:

tr '\r' '\n' < inputfile > outputfile

--
Send responses to the relevant news group rather than email to me.
E-mail sent to this address may be devoured by my very hungry SPAM
filter. Due to Google's refusal to prevent spammers from posting
messages through their servers, I often ignore posts from Google
Groups. Use a real news client if you want me to see your posts.

JR
Reply With Quote
  #4  
Old 11-13-2008, 05:03 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

Michael, Jolly and Vladimir: Thank you very much for your replies! In
my case, I found Jolly's suggestion, "tr '\r' '\n' < inputfile >
outputfile", works the best.

--Roland

On Nov 13, 3:25*am, Vladimir Usenko wrote:
> qquito ÐÉÛÅÔ:
>
>
>
> > Dear Everyone:

>
> > I am using a "Mac OS X" machine and work in an X11 window.

>
> > I got an ASCII file from someone, and when I "more" the file, the
> > entire file shows up as one continuous stream interpersed with ^M
> > (Control-M which is the line-separator). Now I can use the following
> > command

>
> > * % sed '/s/^M//g' filename > newfilename

>
> > to get rid of the ^M signs, but the resulted new file is in one
> > continuous line. My intention, however, is to replace each ^M with a
> > hard return.

>
> > So my question: How do I represent the "hard return" in the above
> > "sed" command? (which needs to be put between the last two slashes in
> > the above command.)

>
> > Thank you for reading and replying!

>
> * col -bx < file > newfile
> But I don't known that exist `col' on MacOS or not.
>
> --
> UV-RIPE


Reply With Quote
  #5  
Old 11-13-2008, 08:30 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

In article ,
Vladimir Usenko wrote:

> col -bx < file > newfile
> But I don't known that exist `col' on MacOS or not.


It does indeed. Learn something new every day. "Half-reverse linefeed"
indeed. I've never been a troff guy.

<http://developer.apple.com/documenta...ManPages/man1/
col.1.html>
--
W. Oates
Reply With Quote
  #6  
Old 11-13-2008, 12:26 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

In article ,
Vladimir Usenko wrote:

> qquito Ô˯ÂÚ:
> > Dear Everyone:
> >
> > I am using a "Mac OS X" machine and work in an X11 window.
> >
> > I got an ASCII file from someone, and when I "more" the file, the
> > entire file shows up as one continuous stream interpersed with ^M
> > (Control-M which is the line-separator). Now I can use the following
> > command
> >
> > % sed '/s/^M//g' filename > newfilename
> >
> > to get rid of the ^M signs, but the resulted new file is in one
> > continuous line. My intention, however, is to replace each ^M with a
> > hard return.
> >
> > So my question: How do I represent the "hard return" in the above
> > "sed" command? (which needs to be put between the last two slashes in
> > the above command.)
> >
> > Thank you for reading and replying!
> >

>
> col -bx < file > newfile
> But I don't known that exist `col' on MacOS or not.


Open Terminal.app and type "man col".

It's there.
Reply With Quote
  #7  
Old 11-13-2008, 01:21 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

Steve Hix ÐÉÛÅÔ:
> In article ,
> Vladimir Usenko wrote:
>
>> qquito Äù?ôË:
>>> Dear Everyone:
>>>
>>> I am using a "Mac OS X" machine and work in an X11 window.
>>>
>>> I got an ASCII file from someone, and when I "more" the file, the
>>> entire file shows up as one continuous stream interpersed with ^M
>>> (Control-M which is the line-separator). Now I can use the following
>>> command
>>>
>>> % sed '/s/^M//g' filename > newfilename
>>>
>>> to get rid of the ^M signs, but the resulted new file is in one
>>> continuous line. My intention, however, is to replace each ^M with a
>>> hard return.
>>>
>>> So my question: How do I represent the "hard return" in the above
>>> "sed" command? (which needs to be put between the last two slashes in
>>> the above command.)
>>>
>>> Thank you for reading and replying!
>>>

>> col -bx < file > newfile
>> But I don't known that exist `col' on MacOS or not.

>
> Open Terminal.app and type "man col".
>
> It's there.

I was saying that "I dunno about it" because I not have access and not
working with MacOS... (I don't clean all newsgroup before sending answer).

--
UV-RIPE
Reply With Quote
  #8  
Old 11-13-2008, 02:00 PM
Database Newbie
 
Join Date: Nov 2008
Posts: 1
enderst-short is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

Quote:
Originally Posted by qquito View Post
Dear Everyone:

I am using a "Mac OS X" machine and work in an X11 window.

I got an ASCII file from someone, and when I "more" the file, the
entire file shows up as one continuous stream interpersed with ^M
(Control-M which is the line-separator). Now I can use the following
command

% sed '/s/^M//g' filename > newfilename

to get rid of the ^M signs, but the resulted new file is in one
continuous line. My intention, however, is to replace each ^M with a
hard return.

So my question: How do I represent the "hard return" in the above
"sed" command? (which needs to be put between the last two slashes in
the above command.)

Thank you for reading and replying!

--Roland
Code:
sed -i 's/^M//' filename
to get the ^M you have to type ctrl+v+m
Reply With Quote
  #9  
Old 11-13-2008, 02:18 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

On Wed, 12 Nov 2008 23:15:45 -0800 (PST), qquito wrote:
> Dear Everyone:


> I am using a "Mac OS X" machine and work in an X11 window.


> I got an ASCII file from someone, and when I "more" the file, the
> entire file shows up as one continuous stream interpersed with ^M
> (Control-M which is the line-separator). Now I can use the following
> command


> % sed '/s/^M//g' filename > newfilename


> to get rid of the ^M signs, but the resulted new file is in one
> continuous line. My intention, however, is to replace each ^M with a
> hard return.


> So my question: How do I represent the "hard return" in the above
> "sed" command? (which needs to be put between the last two slashes in
> the above command.)


The ^M is already a hard return. The problem is that Mac OS X is a Unix
system, and like all Unix systems, it uses line feeds, not carriage
returns, to mark the ends of lines. A command that will do what you want
is:

tr '\r' '\n' newfile

which says to convert all carriage returns into line feeds.

--
Dave Seaman
Third Circuit ignores precedent in Mumia Abu-Jamal ruling.

Reply With Quote
  #10  
Old 11-13-2008, 03:47 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How to replace ^M with a Hard Return with "sed"?

In article ,
Vladimir Usenko wrote:

> Steve Hix Ô˯ÂÚ:
> > In article ,
> > Vladimir Usenko wrote:
> >
> >> qquito ”õ?íÍ:
> >>> Dear Everyone:
> >>>
> >>> I am using a "Mac OS X" machine and work in an X11 window.
> >>>
> >>> I got an ASCII file from someone, and when I "more" the file, the
> >>> entire file shows up as one continuous stream interpersed with ^M
> >>> (Control-M which is the line-separator). Now I can use the following
> >>> command
> >>>
> >>> % sed '/s/^M//g' filename > newfilename
> >>>
> >>> to get rid of the ^M signs, but the resulted new file is in one
> >>> continuous line. My intention, however, is to replace each ^M with a
> >>> hard return.
> >>>
> >>> So my question: How do I represent the "hard return" in the above
> >>> "sed" command? (which needs to be put between the last two slashes in
> >>> the above command.)
> >>>
> >>> Thank you for reading and replying!
> >>>
> >> col -bx < file > newfile
> >> But I don't known that exist `col' on MacOS or not.

> >
> > Open Terminal.app and type "man col".
> >
> > It's there.

> I was saying that "I dunno about it" because I not have access and not
> working with MacOS... (I don't clean all newsgroup before sending answer).


Not a problem. I just noted that it was in MacOS X, and where one could
get use information at the same time.
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 11:02 PM.