Oct 18
Discovered that the bash shell has a variable called $RANDOM, which outputs a pseudo-random number every time you call it. Sweet! Allowed me to randomize the lines in a file for a process I needed to do, thusly:
for i in `cat unusual.txt`; do echo “$RANDOM $i”; done | sort | sed -r ’s/^[0-9]+ //’ > randorder.txt
In other words, put a random number on every line, sort the file, then take off the random numbers. Worked like a charm.
October 18th, 2007 at 10:18 pm
Now that’s clever. I’ll have to remember that.
December 12th, 2007 at 11:48 am
sed: illegal option — r
December 12th, 2007 at 12:17 pm
Red: I used GNU sed on Linux, what Unix are you using?
December 12th, 2007 at 12:29 pm
I’m using bash on osx Leopard
December 12th, 2007 at 12:34 pm
my bad - i made a typo.
December 12th, 2007 at 12:36 pm
Red: no prob
It occurs to me that this thing has a problem whereby if the text file you were randomizing started with numbers and blank, the regular expression could be a bit too greedy. Just a quick hack that I came up with that could be improved.
December 12th, 2007 at 12:41 pm
looks like it’s sed -E on mac
January 29th, 2008 at 4:38 pm
If you really want to be sure that contents of the file (eg. you have lot of lines that start with number) don’t have effect on sorting, you should do something like this:
for i in `cat unusual.txt`; do echo “$RANDOM $i”; done; sed ’s/^/0000/’ | sed ’s/^0*\([0-9]\{5\}[ ].*$\)/\1/’ | sort | sed -r ’s/^[0-9]+ //’ > randorder.txt
btw. this page if first Google hit when you search for .
February 6th, 2008 at 5:00 am
why don’t you just use shuf? like
shuf unusual.txt > randorder.txt
March 1st, 2008 at 4:25 pm
Well I would say that using shuf would be quite an easier way to do it…
But just as a comment on the original method, I had a bit of trouble getting it to work when the lines of my files contained spaces, causing each seperated word to get a line of its own..
Anywho, don’t know if this is just on my setup / or that particular file, but I found a way that worked, very much inspired by your command - and it is as follows:
while read -r line; do echo “$RANDOM $line”; done rand.txt
Cheers… c”,)
March 10th, 2008 at 10:07 pm
Shuf doesn’t exist on the box i have. and i can’t seem to get:
for i in `cat unusual.txt`; do echo “$RANDOM $i”; done | sort | sed -r ’s/^[0-9]+ //’ > randorder.txt
keep getting the sed man page popping up.
and i don’t really understand how:
while read -r line; do echo “$RANDOM $line”; done rand.txt
reads in a file in the first place? where is the input?
March 10th, 2008 at 10:49 pm
My bad, it wasn’t pasting right into the terminal, it works. (original solution)
May 15th, 2008 at 10:21 pm
More efficient way (compared to the for loop)
cat unusual.txt | while read line do….
May 17th, 2008 at 4:01 pm
How about:
sort -R unusual.txt > random.txt
or printing just one random line:
sort -R unusual.txt | tail -1