Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-11-2004, 03:35 PM
|
#1
|
Member
Registered: Aug 2004
Location: Chicago
Distribution: Debian testing/unstable
Posts: 82
Rep:
|
Simple sh script: rand reorder
I *SHOULD* be able to do this myself, but all the ways I can think of are extremely messy:
I need a shell script (or a C program, or a perl script, or whatever) that will randomly reorder the lines in a file. So simple, but I can't seem to do it in less than 20 or so lines.
i.e.:
cat dictionary.txt | reorder.sh > randomwords.txt
or maybe:
reorder.sh "`cat dictionary.txt`" > randomwords.txt
Where dictionary.txt looks like:
aardvark
aardwolf
...
zz top
Any ideas? Seems like it should already exist, or at least be a simple application of sort, or $RANDOM, or something like that. In the interim, I've been sorting by the fifth character, which is fairly random-looking, but I really need it to be different every time.
|
|
|
09-11-2004, 04:39 PM
|
#2
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
Here is an example in Perl (maybe not the more efficient, though) :
Code:
#!/usr/bin/perl
my $file_orig = shift or die "no input file given";
my $file_new = shift or die "no output file given";
open FILE, $file_orig or die "can't open $file_orig";
my @lines = <FILE>;
close FILE;
my $temp = 0;
open FILE, ">", $file_new or die "can't open $file_new to write in";
while(@lines) {
$temp = rand($#lines);
print FILE $lines[$temp];
splice(@lines, $temp, 1);
}
close FILE;
So you call it as this :
./reorder.pl dictionary.txt randomwords.txt
|
|
|
09-11-2004, 05:24 PM
|
#3
|
Member
Registered: Aug 2004
Location: Chicago
Distribution: Debian testing/unstable
Posts: 82
Original Poster
Rep:
|
Thanks a bunch! That looks great, I just don't know enough perl to write something like that myself (read: I know virtually no perl). I made a very slight modification to allow me to use it in pipelines and, more importantly, without reading/writing to disk.
Code:
#!/usr/bin/perl
my @lines = <STDIN>;
my $temp = 0;
while(@lines) {
$temp = rand($#lines);
print $lines[$temp];
splice(@lines, $temp, 1);
}
A call would be:
$ cat dictionary.txt | reorder2.pl > randomwords.txt
Thanks again!
|
|
|
All times are GMT -5. The time now is 08:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|