From: Srijayanth Sridhar Date: 2009-04-02T13:59:43+09:00 Subject: Re: Read and modify logs --00504502ccc34f512d04668b4ee1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit There's at least a couple of ways to remove duplicate lines. 1) results.uniq! 2) results.each { |l| b<wrote: > I'm trying to read the entries in a logfile so I can enter its contents > in a database, the lines are results I want to record, but in order to > be sure that I don't have duplicates I'd like to be able to remove > entries that I have read. Is there an easy way to do this in Ruby so I > can remove read lines from a file that is open once they've been > captured? > > I'm not as worried about the conn_to_db.print line, that is something I > have working ok, so if it looks funny please ignore it. > > What I have so far, and its untested as of yet as I am trying to get the > form right, until I am sure its working . > > Example: > while running > sleep 10 > # read file into an array > f = file.open("mylogfile.log") > saved = File.open("archive.log", "a") > results = f.readlines > # Is there a way to remove everything that's been read here? > f.close > results.each_line do |sending| > conn_to_db.print("data #{sending}") > # Make sure we keep a backup > saved.puts("#{sending}") s.flush > end > # Close the archive file > saved.close > end > > Thanks for any help! > -- > Posted via http://www.ruby-forum.com/. > > --00504502ccc34f512d04668b4ee1--