[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

create file with LF (not CRLF) in windows

Ben Anderson

12/29/2005 10:22:00 PM

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


5 Answers

Bill Kelly

12/29/2005 10:46:00 PM

0

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




John W Kennedy

12/29/2005 10:47:00 PM

0

Ben Anderson wrote:
> 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?

Just as in many other languages, use "wb" instead of "w".

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"

Ben Anderson

12/29/2005 11:11:00 PM

0

yes - there it is. Thanks Bill.

On 12/29/05, Bill Kelly <billk@cts.com> wrote:
> 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
>
>
>
>


Jason Sweat

12/30/2005 1:35:00 AM

0

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


Malte Milatz

12/30/2005 8:08:00 PM

0

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