From: Singeo Date: 2007-05-17T18:16:49+09:00 Subject: Re: Problem removing new line characters on Mac OS X 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('') rssfile.puts('') rssfile.puts(' ') rssfile.puts(' http://app.nea.gov.sg/psi/') rssfile.puts(' Singapore PSI Readings') #rssfile.puts(' Singapore PSI Readings' + Time.now.rfc2822 + '') rssfile.puts(' ' + Time.now.rfc2822 + '') rssfile.puts(' singeo@singeo.com.sg') File.open('psiregions.xml', 'r') do |f1| while line = f1.gets line=line.strip line=line.chomp line.gsub!(/[\n]/, "") line.gsub!(//, "") line.gsub!(/<\/root>/, "") line.gsub!(//, "") line.gsub!(/<\/record>/, "") line.gsub!("24-hr", "Singapore 24-hr") line.gsub!("Region", "") line.gsub!("Sulphur Dioxide", "") line.gsub!(//, "") line.gsub!(/<\/region>/,":") line.gsub!(/<psi>/, " PSI Level ") line.gsub!(/<\/psi>/, "") line.gsub!(/<aqd>/, " - ") line.gsub!(/<\/aqd>/, "") line.gsub!(//, "" + Time.now.rfc2822 + "") rssfile.puts line end end rssfile.puts('') rssfile.puts('') rssfile.close On May 17, 4:38 pm, Dan Zwell 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