From: Rob Biedenharn Date: 2009-08-19T00:50:10+09:00 Subject: Re: fromdos dos2unix in ruby On Aug 18, 2009, at 11:21 AM, Robert Klemme wrote: > 2009/8/18 krzysztof cierpisz : >>> >>> Well, you would read from the input file, replace the dos/windows >>> line >>> endings with unix ones and write to the output file. >>> >> >> I tried with following dos2unix.rb script >> >> ###### dos2unix.rb ###################### >> out = File.open(ARGV[1],"w") >> >> File.open(ARGV[0]).each {|line| >> out << line.gsub!(/\r$/,'') You open the file with the default mode of 'r' here so the File class is going to do the line-ending conversion for you. Then you use String#gsub! which returns nil when no changes are made. You are never going to get output this way. >> } >> >> out.close >> ######################################### >> >> this: >> ruby dos2unix.rb u8nl_utf8_tab.dos.txt d >> >> works fine on Linux (d with length 408 bytes) but not on Windows, on >> Windows d is a file with 0 bytes > > You are not closing the File object properly so your output might > never get flushed to disk... > > Cheers > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ Try something like this: buffer = '' File.open(ARGV[1], 'wb') do |out| # open for writing binary File.open(ARGV[0], 'rb') do |in| # open for reading binary while in.read(1024, buffer) # read upto 1024 bytes into buffer out.write buffer.gsub(/\r\n/, "\n") # change ending and write out end end # end of block closes input end # end of block closes output -Rob P.S. This is untested straight from my head. Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com