Substituting New Line Character

I have a bunch of text that I need to inject a newline character. In my
code, I found a way to put a "\r" where I want the carriage return but
it doesn't work. I also tried "\n" and that doesn't do it either. What
character should I inject to make this text go to the next line where I
currently have the "\r" ?
-------------------text ------------------------
<aavalon-bay-communitie/stamford-connecticut-06902-, (203)357-0986/14790
title=Apartments >Avalon Bay Communities</a>
<aavalon-bay-communitie/stamford-connecticut-06902-,
(203)357-0986/</a>-66 Glenbrook Road No. 200
Stamford,-CT-06902
, (203)
357-0986\r<aavalon-grove-luxury-apartment/stamford-connecticut-06901-,
(203)921-1796/14791 title=Apartments >Avalon Grove Luxury Apartments</a>
<aavalon-grove-luxury-apartment/stamford-connecticut-06901-,
(203)921-1796/</a>-180 Broad Street No. 1227
Stamford,-CT-06901
, (203) 921-1796\r<aavalon-harbor-apartment/stamford-connecticut-06902-,
(203)324-0300/14792 title=Apartment Buildings, Furnished Apartments,
Apartments >Avalon Harbor Apartments</a>
<aavalon-harbor-apartment/stamford-connecticut-06902-,
(203)324-0300/</a>-150 Sfield Avenue
Stamford,-CT-06902
, (203) 324-0300

···

-------------------------------------------------

thanks

atomic

--
Posted via http://www.ruby-forum.com/.

There are multiple standards for the end of line character sequence in
text files depending on the platform on which you're running. Windows
uses \r\n, Unix uses \n, and I Macs prior to OSX used \r. Your content
looks like HTML markup though, so depending on where you want your line
breaks in the file and whether you're rending it as raw text or HTML,
you may actually need to insert <br/> tags.

Assuming you will render the file a raw text, the first thing you have
to do is identify what line ending sequence you need in order for your
text to render correct using the tools you support for the task. Most
common text editors on Windows will only handle \r\n correctly, but some
such as TextPad and Write will do the right thing. The platform on
which you run can serve as a guideline for a good first guess if you're
unsure.

Then process your text to insert the line ending sequence of your
choosing. If things still don't render correctly, view your processed
text in something like a hex editor in order to ensure that you actually
processed it correctly.

-Jeremy

···

On 12/13/2010 12:41 PM, A. Mcbomb wrote:

I have a bunch of text that I need to inject a newline character. In my
code, I found a way to put a "\r" where I want the carriage return but
it doesn't work. I also tried "\n" and that doesn't do it either. What
character should I inject to make this text go to the next line where I
currently have the "\r" ?

Thanks Jeremy.
Everythin looks good if I use <br> where I need it and then view it with
a browser. The text I'm dealing with is HTML but I'm trying to get rid
of all the HTML tags and get the raw data into a text file on my Red Hat
machine.

I tried iterating through each line of the HTML text and using:

   j.gsub!("substituton for where I want the new line", "carriage
return")

For "carriage return", I have tried using \r and \n but it puts those in
literally and does not force a carriage return when I open up the file
in VI.

It seems like \n and \r are not working in this case.

atomic

···

--
Posted via http://www.ruby-forum.com/.

They do work actually:

irb(main):001:0> a = "hello world"
=> "hello world"
irb(main):002:0> a.gsub!(" ", "\n")
=> "hello\nworld"
irb(main):003:0> puts a
hello
world
=> nil

As you can see, the string gets the newline character inserted in place
of the space character in this example.

Can you reproduce this problem with a trivial example? I suspect that
if you try that you might find the problem. What's going wrong is not
readily apparent to me with the information available.

-Jeremy

···

On 12/13/2010 02:14 PM, A. Mcbomb wrote:

Thanks Jeremy.
Everythin looks good if I use <br> where I need it and then view it with
a browser. The text I'm dealing with is HTML but I'm trying to get rid
of all the HTML tags and get the raw data into a text file on my Red Hat
machine.

I tried iterating through each line of the HTML text and using:

   j.gsub!("substituton for where I want the new line", "carriage
return")

For "carriage return", I have tried using \r and \n but it puts those in
literally and does not force a carriage return when I open up the file
in VI.

It seems like \n and \r are not working in this case.

thanks Jeremy,

that helped me figure out what I was doing wrong.

I appreciate it!

atomic

Jeremy Bopp wrote in post #968136:

···

On 12/13/2010 02:14 PM, A. Mcbomb wrote:

For "carriage return", I have tried using \r and \n but it puts those in
literally and does not force a carriage return when I open up the file
in VI.

It seems like \n and \r are not working in this case.

They do work actually:

irb(main):001:0> a = "hello world"
=> "hello world"
irb(main):002:0> a.gsub!(" ", "\n")
=> "hello\nworld"
irb(main):003:0> puts a
hello
world
=> nil

As you can see, the string gets the newline character inserted in place
of the space character in this example.

Can you reproduce this problem with a trivial example? I suspect that
if you try that you might find the problem. What's going wrong is not
readily apparent to me with the information available.

-Jeremy

--
Posted via http://www.ruby-forum.com/\.