sort -R命令不会在Linux中随机对行进行排序

12 linux sorting bash

我无法使用--random-sort在Fedora Linux系统上使用sort命令.

一些上下文信息:

$ 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
Run Code Online (Sandbox Code Playgroud)

而且测试:

$ 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's not random (I've tried it multiple times.)
Run Code Online (Sandbox Code Playgroud)

Joa*_*lva 16

它适用于我的Ubuntu 8.04机器.也许问题是randoms的来源.从手册:

--random源= FILE

         get random bytes from FILE (default /dev/urandom)
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)


Bry*_*nta 9

从GNU coreutils手册:

通过散列输入键进行排序,然后对哈希值进行排序.随机选择哈希函数,确保它没有冲突,以便不同的键具有不同的哈希值.这就像输入的随机排列(参见shuf调用),除了具有相同值的键排序在一起.

如果指定了多个随机排序字段,则对所有字段使用相同的随机散列函数.要为不同的字段使用不同的随机散列函数,您可以多次调用排序.

GNU sort -R采用相同的输入并将其整合在一起; 该线程可能提供一些替代方案:如何使用Red Hat Linux上的标准工具随机化文件中的行?