sort -R不是我操作系统中的选项

时间:2015-10-07 17:55:38

标签: sorting

我有一些操作系统没有排序-R从我有的txt文件生成一个随机列表。例如,我正在尝试使用以下命令:

sort -R file | head -20000 > newfile

我查看了这些操作系统中的手册页,果然,未列出-R选项。

什么是可以从文件生成随机列表并打印到新文件的替代方法?

CentOS 5

3 个答案:

答案 0 :(得分:1)

尝试:

shuf file | head -n 20000 > newfile

或:

cat file | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);'

答案 1 :(得分:0)

您可以使用shuf命令(如果已安装)。

shuf可以将文件作为输入

shuf file | head -n 20000 > newfile

或从stdin读取

cat file | shuf | head -n 20000 > newfile

答案 2 :(得分:0)

cat file | awk 'BEGIN{srand();}{print rand()"\t"$0}' | sort -k1 -n | cut -f2 | head -20000 > newfile

这对我有用。

cat ALLEMAILS.txt | awk 'BEGIN{srand();}{print rand()"\t"$0}' | sort -k1 -n | cut -f2 | head -20000 | tee 20000random.txt

这是为了看到进步。