Create file with LF (not CRLF) in windows


#1

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 A.@andersonbd1 /cygdrive/c/ruby
$ ruby steel.rb

Ben A.@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


#2

From: “Ben A.” removed_email_address@domain.invalid

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


#3

yes - there it is. Thanks Bill.


#4

On 12/29/05, Ben A. removed_email_address@domain.invalid 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


#5

Ben A. wrote:

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

Ben A.@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”.


#6

Bill K.:

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

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

Malte


#7

From: “Malte M.” removed_email_address@domain.invalid

Bill K.:

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