Sort -R command does not randomly sort strings on Linux

I cannot get -random-sort to work with the sort command on a Fedora Linux system.

Some contextual information:

$ cat /etc/fedora-release Fedora release 7 (Moonshine) $ which sort /bin/sort $ man sort | grep -A 2 '\-R' -R, --random-sort sort by random hash of keys $ man sort | grep -A 3 '\-R' -R, --random-sort sort by random hash of keys --random-source=FILE 

And the test:

 $ echo -e "2\n1\n3\n5\n4" 2 1 3 5 4 $ echo -e "2\n1\n3\n5\n4" | sort -r # Sort in reverse order 5 4 3 2 1 $ echo -e "2\n1\n3\n5\n4" | sort -R # Sort in random order 1 2 3 4 5 $ # Fail! That not random (I've tried it multiple times.) 
+7
sorting linux bash
source share
4 answers

It works on my Ubuntu 8.04 machine. Maybe the problem is the source of the radmoms. From the manual:

- random source = FILE

  get random bytes from FILE (default /dev/urandom) 
 # this should give always the same result: echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/zero # this should be random: echo -e '2\n1\n3\n5\n4' | sort -R --random-source=/dev/urandom 
+14
source share

In the GNU coreutils manual:

Sort by hashing the input keys and then sorting the hash values. Choose a hash function arbitrarily, ensuring that it has no collisions, so different keys have different hash values. This is similar to randomly rearranging inputs (see shuf call), except that keys with the same value are sorted together .

If multiple random sort fields are specified, the same random hash function is used for all fields. To use different random hash functions for different fields, you can call sorting more than once.

GNU sort -R accepts the same input and concatenates it; This thread may offer several alternatives: How can I randomize lines in a file using standard tools in Red Hat Linux?

+8
source share

I don’t know if bash works this way, but in ksh there is a β€œwhere” command that tells you what exactly will be executed if you have to enter an argument as a command, while β€œwhat” just tells you the first command instance in $ PATH For example:

 wembley 0 /home/jj33 > which ls /bin/ls wembley 0 /home/jj33 > whence ls '/bin/ls -FC' 

I doubt this is your problem, but the next troubleshooting step would be to specify the exact path (or to avoid a possible alias with a backslash) to β€œsort” during its execution:

 $ echo -e "2\n1\n3\n5\n4" | /bin/sort -R 

After that, I can suspect an environment or locale setting that makes it unstable. Not necessarily important, but LC_ * variables often have unexpected side effects (the first thing I do in a new window is set LC_ALL = C to disable all this)).

+2
source share

I know this is an old post, but this solution works very well ... just in case, someone is looking

 dir='/home/path-to-folder' cd $dir file=`ls |sort -R |tail --lines=1` echo $file 
+1
source share

All Articles