LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-11-2004, 03:35 PM   #1
Sinope
Member
 
Registered: Aug 2004
Location: Chicago
Distribution: Debian testing/unstable
Posts: 82

Rep: Reputation: 15
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.
 
Old 09-11-2004, 04:39 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 09-11-2004, 05:24 PM   #3
Sinope
Member
 
Registered: Aug 2004
Location: Chicago
Distribution: Debian testing/unstable
Posts: 82

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
How to capture & reorder the packets between two PC's Janarthanan Linux - Networking 2 09-04-2005 02:25 AM
reorder partition lables Rojahon Linux - Hardware 2 07-09-2005 06:29 PM
Need help using rand() in C KneeLess Programming 12 10-01-2003 12:51 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration