[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How do you sort a text file?

Danny Abc

1/5/2006 9:22:00 PM

I'm new to Ruby and was wondering how to sort a text file?

With UNIX scripts, I just use "cat input.txt | sort > output.txt".


--
Posted via http://www.ruby-....


16 Answers

Ara.T.Howard

1/5/2006 9:28:00 PM

0

Ross Bamford

1/5/2006 11:48:00 PM

0

On Thu, 05 Jan 2006 21:28:08 -0000, <ara.t.howard@noaa.gov> wrote:

> On Fri, 6 Jan 2006, Danny Abc wrote:
>
>> I'm new to Ruby and was wondering how to sort a text file?
>>
>> With UNIX scripts, I just use "cat input.txt | sort > output.txt".
>
>
> open("output.txt"){|f| f.write IO::readlines("input.txt").sort}
>
> -a

Alternatively, sticking with the command-line thing, how about:

cat input.txt | ruby -e 'puts $stdin.sort' > output.txt

--
Ross Bamford - rosco@roscopeco.remove.co.uk

William James

1/6/2006 5:16:00 AM

0

Danny Abc wrote:
> I'm new to Ruby and was wondering how to sort a text file?
>
> With UNIX scripts, I just use "cat input.txt | sort > output.txt".


ruby -e 'puts ARGF.sort' input1.txt input2.txt input3.txt

Robert Klemme

1/6/2006 8:48:00 AM

0

Danny Abc wrote:
> I'm new to Ruby and was wondering how to sort a text file?
>
> With UNIX scripts, I just use "cat input.txt | sort > output.txt".

You are entitled to the "useless cat award". :-) Any reason why you don't
just do
"sort input.txt > output.txt"?

robert

Hal E. Fulton

1/6/2006 8:53:00 AM

0

Robert Klemme wrote:
> Danny Abc wrote:
>
>>I'm new to Ruby and was wondering how to sort a text file?
>>
>>With UNIX scripts, I just use "cat input.txt | sort > output.txt".
>
>
> You are entitled to the "useless cat award". :-)

My neighbor's cat won that already.

> Any reason why you don't
> just do
> "sort input.txt > output.txt"?

Regarding his original question, I think if he has large files to
sort, he might be well off just using the Unix sort utility. If he
has complex logic, of course, he can still control it all in Ruby.

I haven't tested it, but I can't help expecting that on a large
file, system("sort...") would be the efficient way.


Hal



Robert Klemme

1/6/2006 10:13:00 AM

0

Hal Fulton wrote:
> Robert Klemme wrote:
>> Danny Abc wrote:
>>
>>> I'm new to Ruby and was wondering how to sort a text file?
>>>
>>> With UNIX scripts, I just use "cat input.txt | sort > output.txt".
>>
>>
>> You are entitled to the "useless cat award". :-)
>
> My neighbor's cat won that already.

Oh, on what basis? Does it catch no mice?

> > Any reason why you don't
>> just do
>> "sort input.txt > output.txt"?
>
> Regarding his original question, I think if he has large files to
> sort, he might be well off just using the Unix sort utility. If he
> has complex logic, of course, he can still control it all in Ruby.

+1

> I haven't tested it, but I can't help expecting that on a large
> file, system("sort...") would be the efficient way.

+1

robert

Michal Suchanek

1/6/2006 11:59:00 AM

0

On 1/6/06, Robert Klemme <bob.news@gmx.net> wrote:> Danny Abc wrote:> > I'm new to Ruby and was wondering how to sort a text file?> >> > With UNIX scripts, I just use "cat input.txt | sort > output.txt".>> You are entitled to the "useless cat award". :-) Any reason why you don't> just do> "sort input.txt > output.txt"?Well, cat does not write its arguments so you are pretty sure it isthe input argument whatever command you put next in the pipeline.Plus there was a shell once where input redirection did not work andone replaced it with cat and |. At least that is how I learned to useuseless cats extensively.Anyway, cats are nice :)ThanksMichal-- Support the freedom of music!Maybe it's a weird genre .. but weird is *not* illegal.Maybe next time they will send a special forces commandoto your picnic .. because they think you are weird. www.music-versus-guns.org http://en.police...

Robert Klemme

1/6/2006 1:52:00 PM

0

Michal Suchanek wrote:
> On 1/6/06, Robert Klemme <bob.news@gmx.net> wrote:
>> Danny Abc wrote:
>>> I'm new to Ruby and was wondering how to sort a text file?
>>>
>>> With UNIX scripts, I just use "cat input.txt | sort > output.txt".
>>
>> You are entitled to the "useless cat award". :-) Any reason why you
>> don't just do
>> "sort input.txt > output.txt"?
>
> Well, cat does not write its arguments so you are pretty sure it is
> the input argument whatever command you put next in the pipeline.

I'm sorry, what do you mean by that? Do you mean to say that cat only
reads and so there is no danger of overwriting a file? Unix command line
tools that acutally write to a file named on the command line are rather
seldom; there are quite a few that use an option for that. Never occurred
to me that this extra level of security was needed.

> Plus there was a shell once where input redirection did not work and
> one replaced it with cat and |. At least that is how I learned to use
> useless cats extensively.

You don't need a shell without redirection - sort sorts the files named on
its command line. I doubt it ever behaved differently.

> Anyway, cats are nice :)

Certainly! See http://www.flickr.com/photos/99776024@N00/set...

Kind regards

robert

J. Ryan Sobol

1/6/2006 3:17:00 PM

0

http://catsin...

~ ryan ~


On Jan 6, 2006, at 8:52 AM, Robert Klemme wrote:

>> Anyway, cats are nice :)
>
> Certainly! See http://www.flickr.com/photos/99776024...
> 1697461/

Robert Klemme

1/6/2006 4:47:00 PM

0

J. Ryan Sobol wrote:
> http://catsin...

Uh, oh, need to upload a picture of our "Tyron in the Sink". :-)

robert


> ~ ryan ~
>
>
> On Jan 6, 2006, at 8:52 AM, Robert Klemme wrote:
>
>>> Anyway, cats are nice :)
>>
>> Certainly! See http://www.flickr.com/photos/99776024...
>> 1697461/