From: Robert Dober Date: 2009-08-19T01:42:24+09:00 Subject: Re: fromdos dos2unix in ruby On Tue, Aug 18, 2009 at 5:50 PM, Rob Biedenharn wrote: > > 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 fancy little bug here Rob, do you spot it? What if \r is the 1024th char? This will happen, one day ;) Unless the file is hugh I would try File.open.... File.open ... out.print in.read.gsub( /\r\n/, /\n/ ) end end If performance can be an issue we could use File#each with 10.chr as a seperator in.each 10.chr do | line | out.print line.sub( /\r\n\z/, 10.chr ) end HTH Robert -- module Kernel alias_method :ë, :lambda end