bash sort -R 命令不会在 Linux 中随机排序行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/537191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The sort -R command doesn't sort lines randomly in Linux
提问by Joao da Silva
I can't get --random-sort to work with the sort command on a Fedora Linux-system.
我无法让 --random-sort 在 Fedora Linux 系统上使用 sort 命令。
Some context 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's not random (I've tried it multiple times.)
回答by Joao da Silva
It works on my Ubuntu 8.04 machine. Maybe the problem is the source of randoms. From the manual:
它适用于我的 Ubuntu 8.04 机器。也许问题是随机数的来源。从手册:
--random-source=FILE
get random bytes from FILE (default /dev/urandom)
--random-source=文件
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
回答by Bryce Guinta
From the GNU coreutils manual:
来自 GNU coreutils 手册:
Sort by hashing the input keys and then sorting the hash values. Choose the hash function at random, ensuring that it is free of collisions so that differing keys have differing hash values. This is like a random permutation of the inputs (see shuf invocation), except that keys with the same value sort 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 invoke sort more than once.
通过对输入键进行散列然后对散列值进行排序来进行排序。随机选择散列函数,确保它没有冲突,以便不同的键具有不同的散列值。这就像输入的随机排列(参见 shuf 调用),除了具有相同值的键排序在一起。
如果指定了多个随机排序字段,则所有字段都使用相同的随机哈希函数。要对不同的字段使用不同的随机散列函数,您可以多次调用 sort。
GNU sort -R takes the same input and lumps it together; This thread may offer some alternatives: How can I randomize the lines in a file using standard tools on Red Hat Linux?
GNU sort -R 接受相同的输入并将其合并在一起;该线程可能会提供一些替代方案:如何使用 Red Hat Linux 上的标准工具随机化文件中的行?
回答by jj33
I don't know if bash works this way, but in ksh there's the "whence" command which tells you exactly what will execute if you were to type the argument as a command, whereas "which" just tells you the first instance of the command in $PATH. For instance:
我不知道 bash 是否以这种方式工作,但是在 ksh 中有“whence”命令,它告诉您如果将参数作为命令键入,将会执行什么,而“which”只是告诉您第一个实例$PATH 中的命令。例如:
wembley 0 /home/jj33 > which ls
/bin/ls
wembley 0 /home/jj33 > whence ls
'/bin/ls -FC'
I doubt it's your problem, but a next troubleshooting step would be to specify the exact path (or escape a possible alias with a backslash) for "sort" when you execute it:
我怀疑这是您的问题,但下一个故障排除步骤是在执行“排序”时指定确切路径(或使用反斜杠转义可能的别名):
$ echo -e "2\n1\n3\n5\n4" | /bin/sort -R
After that, I might suspect an environment or locale setting that's making it wonky. Not necessarily important, but the LC_* variables often have unexpected side effects (the first thing I do on a new box is set LC_ALL=C to turn it all off =)).
在那之后,我可能会怀疑环境或语言环境设置使它变得不稳定。不一定重要,但 LC_* 变量通常会产生意想不到的副作用(我在新盒子上做的第一件事是设置 LC_ALL=C 以将其全部关闭 =))。
回答by nuntius
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