Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Problem removing new line characters on Mac OS X

Reply
Thread Tools

Problem removing new line characters on Mac OS X

 
 
Singeo
Guest
Posts: n/a
 
      05-17-2007
Hi, I'm pretty new to Ruby. I've got a text file where I need to
remove some new line characters. I've tried everything I can think of
to do this with no success, including:

line.gsub!("/r","")
line.gsub!("/n","")
line=line.chomp

I can't seem to get the new line character to be recognised and dealt
with. Any advice appreciated.

Thanks

 
Reply With Quote
 
 
 
 
Harry Kakueki
Guest
Posts: n/a
 
      05-17-2007
On 5/17/07, Singeo <> wrote:
> Hi, I'm pretty new to Ruby. I've got a text file where I need to
> remove some new line characters. I've tried everything I can think of
> to do this with no success, including:
>
> line.gsub!("/r","")
> line.gsub!("/n","")
> line=line.chomp
>
> I can't seem to get the new line character to be recognised and dealt
> with. Any advice appreciated.
>
> Thanks
>
>
>


line.chomp! doesn't work?
Would you show some code?

Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/

 
Reply With Quote
 
 
 
 
Dan Zwell
Guest
Posts: n/a
 
      05-17-2007
Singeo wrote:
> Hi, I'm pretty new to Ruby. I've got a text file where I need to
> remove some new line characters. I've tried everything I can think of
> to do this with no success, including:
>
> line.gsub!("/r","")
> line.gsub!("/n","")
> line=line.chomp
>
> I can't seem to get the new line character to be recognised and dealt
> with. Any advice appreciated.
>
> Thanks
>
>
>


It looks like you should be using backslashes. If you want to match both
newlines and carriage returns, you can use:

line.gsub!(/[\n\r]/, "")

-Dan

 
Reply With Quote
 
Hermann Martinelli
Guest
Posts: n/a
 
      05-17-2007
Singeo wrote:
> Hi, I'm pretty new to Ruby. I've got a text file where I need to
> remove some new line characters. I've tried everything I can think of
> to do this with no success, including:
>
> line.gsub!("/r","")
> line.gsub!("/n","")
> line=line.chomp


In case your problem is just your Ruby syntax:

1. Replace the forward slashes (like in "/r") by
backward slashes ("\r" in your above mentioned
solution.

2. Make the first parameter to the gsub! method
a Regexp instead of a string. The API docs say:
"... if it is a String then no regular expression
metacharacters will be interpreted ...".

This is why neither "/r" (1) nor "\r" (2) will
work.



If chomp does not work, you may be using a Mac
file under Linux or Windows. In that case you may
want to try something like

line.gsub!(/\015/, '')


Hermann
 
Reply With Quote
 
Singeo
Guest
Posts: n/a
 
      05-17-2007
Apologies for the mis-understanding, I have been using backslashes.
Here's my code, as you'll see from the resulting file there are a
bunch of new line characters in the file I'd like to get rid of.
Thanks for the help so far.

require("rubygems")
require("scrubyt")
require ("open-uri")
require 'time'
require 'date'

psi = Scrubyt::Extractor.define do
fetch("http://app.nea.gov.sg/psi/")

record("/html/body/div/table/tr/td/table/tbody/tr/td/div",
{ :generalize => true }) do
title("/strong[1]/font[1]")
item("/table/tbody/tr/td/table/tbody/tr", { :generalize => true })
do
region("/td[1]")
psi("/td[7]")
aqd("/td[8]")
end
end
end

f = open("psiregions.xml", File::CREAT|File::TRUNC|File::RDWR) {|f|
psi.to_xml.write(f, 1)
}

# Create the RSS file.
rssfile = File.new("sgpsi.xml", "w")
rssfile.puts('<?xml version="1.0" encoding="UTF-8"?>')
rssfile.puts('<rss version="2.0">')
rssfile.puts(' <channel>')
rssfile.puts(' <link>http://app.nea.gov.sg/psi/</link>')
rssfile.puts(' <description>Singapore PSI Readings</description>')
#rssfile.puts(' <title>Singapore PSI Readings' + Time.now.rfc2822
+ '</title>')
rssfile.puts(' <lastBuildDate>' + Time.now.rfc2822 + '</
lastBuildDate>')
rssfile.puts(' <webMaster></webMaster>')

File.open('psiregions.xml', 'r') do |f1|
while line = f1.gets
line=line.strip
line=line.chomp
line.gsub!(/[\n]/, "")
line.gsub!(/<root>/, "")
line.gsub!(/<\/root>/, "")
line.gsub!(/<record>/, "")
line.gsub!(/<\/record>/, "")
line.gsub!("24-hr", "Singapore 24-hr")
line.gsub!("<region>Region</region>", "")
line.gsub!("<region>Sulphur Dioxide</region>", "")
line.gsub!(/<region>/, "<title>")
line.gsub!(/<\/region>/,":")
line.gsub!(/<psi>/, " PSI Level ")
line.gsub!(/<\/psi>/, "")
line.gsub!(/<aqd>/, " - ")
line.gsub!(/<\/aqd>/, "</title>")
line.gsub!(/<item>/, "<item><pubDate>" + Time.now.rfc2822 + "</
pubDate>")
rssfile.puts line
end
end


rssfile.puts('</channel>')
rssfile.puts('</rss>')
rssfile.close


On May 17, 4:38 pm, Dan Zwell <dzw...@gmail.com> wrote:
> Singeo wrote:
> > Hi, I'm pretty new to Ruby. I've got a text file where I need to
> > remove some new line characters. I've tried everything I can think of
> > to do this with no success, including:

>
> > line.gsub!("/r","")
> > line.gsub!("/n","")
> > line=line.chomp

>
> > I can't seem to get the new line character to be recognised and dealt
> > with. Any advice appreciated.

>
> > Thanks

>
> It looks like you should be using backslashes. If you want to match both
> newlines and carriage returns, you can use:
>
> line.gsub!(/[\n\r]/, "")
>
> -Dan



 
Reply With Quote
 
Singeo
Guest
Posts: n/a
 
      05-17-2007
Hi Hermann, just tried your suggestion of:

line.gsub!(/\015/, '')

still no success. I'm creating and running the file on a Mac.

On May 17, 5:05 pm, Hermann Martinelli <martine...@yahoo.com> wrote:
> Singeo wrote:
> > Hi, I'm pretty new to Ruby. I've got a text file where I need to
> > remove some new line characters. I've tried everything I can think of
> > to do this with no success, including:

>
> > line.gsub!("/r","")
> > line.gsub!("/n","")
> > line=line.chomp

>
> In case your problem is just your Ruby syntax:
>
> 1. Replace the forward slashes (like in "/r") by
> backward slashes ("\r" in your above mentioned
> solution.
>
> 2. Make the first parameter to the gsub! method
> a Regexp instead of a string. The API docs say:
> "... if it is a String then no regular expression
> metacharacters will be interpreted ...".
>
> This is why neither "/r" (1) nor "\r" (2) will
> work.
>
> If chomp does not work, you may be using a Mac
> file under Linux or Windows. In that case you may
> want to try something like
>
> line.gsub!(/\015/, '')
>
> Hermann



 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-17-2007
<snip>

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      05-17-2007
Singeo wrote:
> Here's my code, as you'll see from the resulting file there are a
> bunch of new line characters in the file I'd like to get rid of.
> [...]
> line=line.chomp
> [...]
> rssfile.puts line


puts adds a newline to the end of the string it writes. If you don't want that
behaviour (which you obviously don't), use print instead.


--
Ist so, weil ist so
Bleibt so, weil war so

 
Reply With Quote
 
Hermann Martinelli
Guest
Posts: n/a
 
      05-17-2007
Singeo wrote:
> Hi Hermann, just tried your suggestion of:
>
> line.gsub!(/\015/, '')
>
> still no success. I'm creating and running the file on a Mac.


Are you shure that you it is not successful?

It would be good to know how you read the lines,
how you (not) remove the carriage returns,
and how you perhaps put the lines together
(adding again \r characters by mistake?).

Are you removing the carriage returns line
by line (in which case the chomp should be perfect)
or are you trying it as a whole, i.e. do you have
not only one line but a whole file in 'line'?

Rather than an answer to these questions I would
prefer to see some more code of the whole part
from opening the file to writing back or putting
out the strings.

Hermann
 
Reply With Quote
 
Singeo
Guest
Posts: n/a
 
      05-17-2007
Hermann, I followed Sebatian's advice to use print instead of puts and
that solved my problem. But I would still like to understand how to
remove the newline characters. Here's my code as it currently stands
(with "rssfile.print line" in place of "rssfile.puts line"), hopefully
it will help you see how I was trying to tackle the problem.

require("rubygems")
require("scrubyt")
require ("open-uri")
require 'time'
require 'date'

psi = Scrubyt::Extractor.define do
fetch("http://app.nea.gov.sg/psi/")

record("/html/body/div/table/tr/td/table/tbody/tr/td/div",
{ :generalize => true }) do
title("/strong[1]/font[1]")
item("/table/tbody/tr/td/table/tbody/tr", { :generalize => true })
do
region("/td[1]")
psi("/td[7]")
aqd("/td[8]")
end
end
end

f = open("psiregions.xml", File::CREAT|File::TRUNC|File::RDWR) {|f|
psi.to_xml.write(f, 1)
}

# Create the RSS file.
rssfile = File.new("sgpsi.xml", "w")
rssfile.puts('<?xml version="1.0" encoding="UTF-8"?>')
rssfile.puts('<rss version="2.0">')
rssfile.puts(' <channel>')
rssfile.puts(' <link>http://app.nea.gov.sg/psi/</link>')
rssfile.puts(' <description>Singapore PSI Readings</description>')
#rssfile.puts(' <title>Singapore PSI Readings' + Time.now.rfc2822
+ '</title>')
rssfile.puts(' <lastBuildDate>' + Time.now.rfc2822 + '</
lastBuildDate>')
rssfile.puts(' <webMaster></webMaster>')

File.open('psiregions.xml', 'r') do |f1|
while line = f1.gets
line=line.strip
line.gsub!(/<root>/, "")
line.gsub!(/<\/root>/, "")
line.gsub!(/<record>/, "")
line.gsub!(/<\/record>/, "")
line.gsub!("24-hr", "Singapore 24-hr")
line.gsub!("<region>Region</region>", "")
line.gsub!("<region>Sulphur Dioxide</region>", "")
line.gsub!(/<region>/, "<title>")
line.gsub!(/<\/region>/,":")
line.gsub!(/<psi>/, " PSI Level ")
line.gsub!(/<\/psi>/, "")
line.gsub!(/<aqd>/, " - ")
line.gsub!(/<\/aqd>/, "</title>")
line.gsub!(/<item>/, "<item><pubDate>" + Time.now.rfc2822 + "</
pubDate>")
line.gsub!(/<\/item>/, "</item>\n")
rssfile.print line
end
end

rssfile.puts('')
rssfile.puts('</channel>')
rssfile.puts('</rss>')

rssfile.close


On May 17, 6:20 pm, Hermann Martinelli <hermann.martine...@yahoo.com>
wrote:
> Singeo wrote:
> > Hi Hermann, just tried your suggestion of:

>
> > line.gsub!(/\015/, '')

>
> > still no success. I'm creating and running the file on a Mac.

>
> Are you shure that you it is not successful?
>
> It would be good to know how you read the lines,
> how you (not) remove the carriage returns,
> and how you perhaps put the lines together
> (adding again \r characters by mistake?).
>
> Are you removing the carriage returns line
> by line (in which case the chomp should be perfect)
> or are you trying it as a whole, i.e. do you have
> not only one line but a whole file in 'line'?
>
> Rather than an answer to these questions I would
> prefer to see some more code of the whole part
> from opening the file to writing back or putting
> out the strings.
>
> Hermann



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set maximum characters allowed for Single Line and Multi Line Textboxin .Net jayeshsorathia@gmail.com ASP .Net 0 08-23-2012 10:28 AM
To count # characters in a line and make the line shorter clearguy02@yahoo.com Perl Misc 3 05-09-2009 02:36 AM
JSON parsing errors...[looks like thers some problem wid NEw line characters] cool_buddy218@yahoo.com Javascript 0 06-30-2007 09:35 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57