How do you sort a text file?

D

Danny Abc

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".
 
A

ara.t.howard

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
===============================================================================
 
R

Ross Bamford

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
 
W

William James

Danny said:
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
 
R

Robert Klemme

Danny said:
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
 
H

Hal Fulton

Robert said:
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
 
R

Robert Klemme

Hal said:
My neighbor's cat won that already.

Oh, on what basis? Does it catch no mice?
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
 
R

Robert Klemme

Michal said:
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
 
J

Joel VanderWerf

Has the venerable tradition of Friday cat blogging made its way into
ruby-talk? ;)
 
A

Andreas Eder

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
 
J

Jim

Danny said:
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".

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.
 
C

Chad Perrin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top