Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do you sort a text file?

Reply
Thread Tools

How do you sort a text file?

 
 
Danny Abc
Guest
Posts: n/a
 
      01-05-2006
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-forum.com/.


 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      01-05-2006
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
--
================================================== =============================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
================================================== =============================



 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      01-05-2006
On Thu, 05 Jan 2006 21:28:08 -0000, <> 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 -
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      01-06-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".



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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-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".


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

robert

 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      01-06-2006
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



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-2006
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-2006
Michal Suchanek wrote:
> On 1/6/06, 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". 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/sets/1697461/

Kind regards

robert

 
Reply With Quote
 
J. Ryan Sobol
Guest
Posts: n/a
 
      01-06-2006
--Apple-Mail-14--660372295
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

http://catsinsinks.com/

~ ryan ~


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

>> Anyway, cats are nice

>
> Certainly! See http://www.flickr.com/photos/99776024@N00/sets/
> 1697461/



--Apple-Mail-14--660372295--


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-2006
J. Ryan Sobol wrote:
> http://catsinsinks.com/


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@N00/sets/
>> 1697461/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
The Colourised Bewitched -- sort of OK....... sort of! anthony DVD Video 26 06-28-2005 04:39 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57