Forum: Ruby create file with LF (not CRLF) in windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5f699f59b58693ce313995caf4330486?d=identicon&s=25 Ben Anderson (Guest)
on 2005-12-29 23:22
(Received via mailing list)
Hi,
I have a bunch of files that I want to convert from CRLF to just LF.
How might I do this?  I'm testing with the following example file
(steel.rb):

f = File.new("steel", "w")
f.syswrite("steel\ncity");
f.close

I then run this from cygwin and get the following output

Ben Anderson@andersonbd1 /cygdrive/c/ruby
$ ruby steel.rb

Ben Anderson@andersonbd1 /cygdrive/c/ruby
$ cat -v steel
steel^M
city

How might I get rid of the CR and just have ruby use an LF?
Thanks,
Ben
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2005-12-29 23:46
(Received via mailing list)
From: "Ben Anderson" <benanderson.us@gmail.com>
>
> I have a bunch of files that I want to convert from CRLF to just LF.
> How might I do this?  I'm testing with the following example file
> (steel.rb):
>
> f = File.new("steel", "w")
> f.syswrite("steel\ncity");
> f.close

Try in "binary" mode.

f = File.new("steel", "wb")

BTW, if you use blocks, you can omit the f.close:

File.new("steel", "wb") do |f|
  f.print("steel\ncity")
end


(See also IO#binmode )


Regards,

Bill
5f699f59b58693ce313995caf4330486?d=identicon&s=25 Ben Anderson (Guest)
on 2005-12-30 00:11
(Received via mailing list)
yes - there it is.  Thanks Bill.
Dabdaf107905a3605174b18ead82d45d?d=identicon&s=25 Jason Sweat (sweatje)
on 2005-12-30 02:35
(Received via mailing list)
On 12/29/05, Ben Anderson <benanderson.us@gmail.com> wrote:
> yes - there it is.  Thanks Bill.

Perhaps it makes a difference which binary you use, windows vs.
cygwin.  I am usign the cygwin Ruby and it did not exhibit the
behavior you mentioned.

Regards,
Jason
4fea1ef11180adaaa299d503ca6010d0?d=identicon&s=25 John W. Kennedy (Guest)
on 2005-12-31 12:15
(Received via mailing list)
Ben Anderson wrote:
>
> Ben Anderson@andersonbd1 /cygdrive/c/ruby
> $ ruby steel.rb
>
> Ben Anderson@andersonbd1 /cygdrive/c/ruby
> $ cat -v steel
> steel^M
> city
>
> How might I get rid of the CR and just have ruby use an LF?

Just as in many other languages, use "wb" instead of "w".
C3274d0d109830f7f5121a7d8f7ead98?d=identicon&s=25 Malte Milatz (Guest)
on 2005-12-31 12:57
(Received via mailing list)
Bill Kelly:
> File.new("steel", "wb") do |f|
>   f.print("steel\ncity")
> end

Warning: File::new() does not take block; use File::open() instead.

Malte
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2005-12-31 21:24
(Received via mailing list)
From: "Malte Milatz" <malte__@gmx-topmail.de>
> Bill Kelly:
>> File.new("steel", "wb") do |f|
>>   f.print("steel\ncity")
>> end
>
> Warning: File::new() does not take block; use File::open() instead.

Ooops!  Thanks for catching that!    O:-)


Bill
This topic is locked and can not be replied to.