From: Robert Klemme Date: 2012-03-30T01:11:29+09:00 Subject: Re: copy file into new without dups, eol problem Jeremy Bopp wrote in post #1053948: > On 03/29/2012 07:55 AM, Robert Klemme wrote: >> But what about the dups? What constitutes a duplicate? If it is just >> raw content, you could use "sort -u" (standalone command). > > Again from the original example, the records to compare for uniqueness > are simple lines. Of course that simplicity belies the issue of line > endings. ;-) Ah, I overlooked the call to #uniq. I think we should be able to fix the original with a small insertion: File.open("newf.txt", "w+") { |file| file.puts File.readlines("oldf.txt").each(&:chomp!).uniq } Although from an efficiency point of view another approach would be preferable: File.open "oldf.txt" do |in| File.open "newf.txt", "w" do |out| last = nil in.each_line do |line| line.chomp! out.puts line unless line == last last = line end end end Note that opening "newf.txt" before opening "oldf.txt" will lead to an empty file being written in case "oldf.txt" does not exist even though an exception is thrown. > Also, the OP appears to be running on Windows, so "sort -u" is not > available out of the box. Right, I'm so used to cygwin that I keep forgetting not everybody has it installed. :-) Cheers robert -- Posted via http://www.ruby-forum.com/.