Forum: Ruby How do you sort a text file?

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.
300addadf3867ae097240a68985a96c8?d=identicon&s=25 Danny --- (dannyabc)
on 2006-01-05 22:22
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".
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-05 22:30
(Received via mailing list)
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
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-06 00:58
(Received via mailing list)
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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-01-06 09:10
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-06 09:49
(Received via mailing list)
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
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-01-06 09:55
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-06 11:20
(Received via mailing list)
Hal Fulton wrote:
> 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
011737f94b61f527bd869eb54d735f57?d=identicon&s=25 Michal Suchanek (Guest)
on 2006-01-06 13:02
(Received via mailing list)
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.
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.
Anyway, cats are nice :)

Thanks

Michal

--
             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 commando
to your picnic .. because they think you are weird.
 www.music-versus-guns.org  http://en.policejnistat.cz
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-06 14:53
(Received via mailing list)
Michal Suchanek wrote:
> 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
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2006-01-06 16:18
(Received via mailing list)
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-06 17:51
(Received via mailing list)
J. Ryan Sobol wrote:
> http://catsinsinks.com/

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

    robert
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-01-06 18:15
(Received via mailing list)
Has the venerable tradition of Friday cat blogging made its way into
ruby-talk? ;)
7a1de4160e6d3b429508aa394f899c4f?d=identicon&s=25 Andreas Eder (Guest)
on 2006-01-07 15:23
(Received via mailing list)
>>>>> "Danny" == Danny Abc <dannyabc@comcast.net> writes:

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

Why not sort <input.txt >output.txt?
That cat is totally superfluous.

'Andreas
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2006-01-08 00:59
(Received via mailing list)
On Sat, Jan 07, 2006 at 11:23:00PM +0900, Andreas Eder wrote:
> >>>>> "Danny" == Danny Abc <dannyabc@comcast.net> writes:
>
>     Danny> I'm new to Ruby and was wondering how to sort a text file?
>     Danny> With UNIX scripts, I just use "cat input.txt | sort > output.txt".
>
> Why not sort <input.txt >output.txt?
> That cat is totally superfluous.

You don't need that input redirect.  Input takes a filename as an
argument, so you can simply do this:
  sort input.txt > output.txt

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

"Real ugliness is not harsh-looking syntax, but having to
build programs out of the wrong concepts." - Paul Graham
7c3a0706ce454a5e7c405a63fdcd2c21?d=identicon&s=25 Jim (Guest)
on 2006-01-08 09:11
(Received via mailing list)
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".
>
>
> --
> Posted via http://www.ruby-forum.com/.

Why can't I just let these things go?

i use:

$ sort -o outfile.txt infile.txt

No pipes, and you can use the same file name if you don't want to keep
both files.
This topic is locked and can not be replied to.