Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby net-sftp read file line by line

I am using ruby 2.0.0 and rails 4.0.0. I have something similar to this:

require 'net/sftp'


 sftp = Net::SFTP.start('ftp.app.com','username', :password => 'password')

 sftp.file.open("/path/to/remote/file.csv", "r") do |f|
    puts f.gets
 end 

This opens the file on the FTP site, but it only puts the first line of the csv file. I need to read this file row by row, preferably ignoring the header.

How can I read the file row by row, without downloading the file locally?

like image 615
Luigi Avatar asked Mar 20 '23 07:03

Luigi


1 Answers

I solved this by doing this:

data = sftp.download!("/path/to/remote/file.csv").split(/\r\n/)

data.each do |line|
  puts line
end
like image 58
Luigi Avatar answered Mar 28 '23 00:03

Luigi