removing newline from eachline in file

C

Chuck Dawit

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
while (line = f.readline)
line = line =~ /(.*?)\n/
url = "www." + line + ".net"

open(url) { |page| page_content = page.read()
puts "link exists"
}
end
rescue
puts "link does not exist"

rescue EOFError
f.close
end
 
7

7stud --

Chuck said:
I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line?

See String#chomp:

require "open-uri"

File.open("aaa.txt", "w") do |file|
file.print("hello\n", "world\n", "goodbye\n")
end

File.open("aaa.txt") do |file|
file.each do |line|
print line.chomp
end
puts
end

--output:--
helloworldgoodbye
 
T

Tim Hunter

Chuck said:
I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.


ri String#chomp
----------------------------------------------------------- String#chomp
str.chomp(separator=$/) => new_str
------------------------------------------------------------------------
Returns a new String with the given record separator removed from
the end of str (if present). If $/ has not been changed from the
default Ruby record separator, then chomp also removes carriage
return characters (that is it will remove \n, \r, and \r\n).

"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"
"hello\n\r".chomp #=> "hello\n"
"hello\r".chomp #=> "hello"
"hello \n there".chomp #=> "hello \n there"
"hello".chomp("llo") #=> "he"
 
7

7stud --

Chuck said:
Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

My isp redirects faulty urls to an error page, so all urls return a web
page. I think you're going to have to use Net::HTTP to look at the
headers of the response, and then take some action based on the header
value.
 
7

7stud --

Chuck said:
Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
while (line = f.readline)
line = line =~ /(.*?)\n/
url = "www." + line + ".net"

open(url) { |page| page_content = page.read()
puts "link exists"
}
end
rescue
puts "link does not exist"

rescue EOFError
f.close
end

If your program is currently crashing on bad urls, you just need to put
the begin/rescue around the open statement---not outside your loop.
Something like this(untested):

begin
while (line = f.readline)
line = line =~ /(.*?)\n/
url = "www." + line + ".net"

begin
open(url) { |page| page_content = page.read() }
puts "link exists"
rescue Exception
puts "in rescue"
#do nothing -- when you catch an exception the exception
goes away

puts "execution continues on its way"
end
end
 
T

Tom Reilly

Try changing the code to:
file_in = File.new("whatever")
file_in.each do |x|
url = x.chomp + ".net"

gud luck
 
P

Peña, Botp

IyBsaW5lID0gbGluZSAgPX4gLyguKj8pXG4vDQoNCndoYXQgZG9lcyB0aGlzIGxpbmUgb2YgY29k
ZSBkbz8NCg0KDQo=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top