1

I have a list of names (names.txt) separated by line. After I loop through each line, I'd like to move it to another file (processed.txt).

My current implementation to loop through each line:

open("names.txt") do |csv|
  csv.each_line do |line|
    url = line.split("\n")
    puts url
    # Remove line from this file amd move it to processed.txt
  end
end
Onichan
  • 4,476
  • 6
  • 31
  • 60

2 Answers2

1

You can do it like this:

File.open('processed.txt', 'a') do |file|
  open("names.txt") do |csv|
    csv.each_line do |line|
      url = line.chomp
      # Do something interesting with url...
      file.puts url
    end
  end
end

This will result in processed.txt containing all of the urls that were processed with this code.

Note: Removing the line from names.txt is not practical using this method. See How do I remove lines of data in the middle of a text file with Ruby for more information. If this is a real goal of this solution, it will be a much larger implementation with some design considerations that need to be defined.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
  • Thanks. Since removing the line is not practical, would it be possible to add a character to the beginning of the line (to mark it as completed?) – Onichan May 14 '16 at 00:51
  • That is possible, and if the `names.txt` file will be (fairly) small, we could possibly rewrite the file after processing, omitting the first line. Is this file large or is it manageable? – Michael Gaskill May 14 '16 at 01:06
  • Actually, I think ill just write the current line to a new file with each iteration of the loop. Then I'll know which line was processed. Is that a more practical approach? – Onichan May 14 '16 at 01:09
  • It might be. If your script fails while processing, will you need to be able to start where it failed, or is it acceptable to start at the beginning again? I do think it's possible to build what you need, so that you don't need to make a compromise. File size of `names.txt` will determine which is the best approach to take, and I think I can come up with something that works for you if I know how big it will be (lines or bytes). – Michael Gaskill May 14 '16 at 01:13
  • The file size of `names.txt` is around 50mb (30-80mb range). And yes, it needs to start where it failed. It cannot start from the beginning. Thanks! – Onichan May 14 '16 at 07:20
  • @Onichan Thanks for the details. I'll put together a solution that I think will work. – Michael Gaskill May 14 '16 at 07:22
  • Yes, I was thinking of just using `next if index < current_line` where `current_line` is the saved line. But that requires the program to loop through the entire list everytime it fails/stops. Is there a better way to skip to a particular line? – Onichan May 14 '16 at 07:24
  • Nothing better than that. At least that I'm aware of. – Michael Gaskill May 14 '16 at 07:28
1
def readput 
  @names = File.readlines("names.txt") 
  File.open("processed.txt", "w+") do |f| 
    f.puts(@names) 
  end 
end
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
StanisLove Sid
  • 322
  • 3
  • 9