From: Jeremy Bopp Date: 2012-03-29T06:40:41+09:00 Subject: Re: copy file into new without dups, eol problem On 03/28/2012 02:41 PM, Mario Trento wrote: > File.open("newf.txt", "w+") { |file| file.puts > File.readlines("oldf.txt").uniq } > > Hi, > I'm doing it like above on most recent 1.9.3 and I have mess with end of > line char, it's simple ascii file, all runs on windows: > > oldf.txt:::===================================== > 12/30/2011 02:12 AM 5,921,042 01 > 12/30/2011 02:12 AM 5,921,042 01 > 12/30/2011 02:12 AM 5,938,806 02 > 12/30/2011 02:12 AM 5,921,042 01 > 12 AM 5,921,042 01 - Andrea Bocel > > newf.txt:::===================================== > 12/30/2011 02:12 AM 5,921,042 01 12/30/2011 02:12 AM > 5,938,806 02 12 AM 5,921,042 01 - Andrea Bocel > > Is there any option I should check?? File.readlines opens the file with mode 'r' by default. On Windows, this means to open the file in text read mode, and that will convert Windows line endings into Unix line endings for each line that is read in from oldf.txt. When you write the array of filtered lines to newf.txt, that first joins the lines together with an empty string between them and then writes the result to the file. I think that prevents the embedded Unix line endings from being converted to Windows line endings. I don't have a convenient Windows system on which to try this, but I think the easy solution since you're on Ruby 1.9.3 would be to tell File.readlines to open the file in binary read mode, AKA 'rb': File.readlines("oldf.txt", mode: "rb") -Jeremy