Removing newline from eachline in file

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

Chuck D. wrote:

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

Chuck D. wrote:

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"

Chuck D. wrote:

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.

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

gud luck

Chuck D. wrote:

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

line = line =~ /(.*?)\n/

what does this line of code do?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs