如何使用 Red Hat Linux 上的标准工具随机化文件中的行? [英] How can I randomize the lines in a file using standard tools on Red Hat Linux?
问题描述
如何使用 Red Hat Linux 上的标准工具随机化文件中的行?
How can I randomize the lines in a file using standard tools on Red Hat Linux?
我没有 shuf
命令,所以我正在寻找类似 perl
或 awk
的单行代码来完成同样的任务.
I don't have the shuf
command, so I am looking for something like a perl
or awk
one-liner that accomplishes the same task.
推荐答案
还有一个 Perl 单行程序!
And a Perl one-liner you get!
perl -MList::Util -e 'print List::Util::shuffle <>'
它使用一个模块,但该模块是 Perl 代码分发的一部分.如果这还不够好,您可以考虑自己动手.
It uses a module, but the module is part of the Perl code distribution. If that's not good enough, you may consider rolling your own.
我尝试将它与 -i
标志(就地编辑")一起使用,让它编辑文件.文档表明它应该可以工作,但事实并非如此.它仍然将洗牌后的文件显示到标准输出,但这次它删除了原始文件.我建议你不要使用它.
I tried using this with the -i
flag ("edit-in-place") to have it edit the file. The documentation suggests it should work, but it doesn't. It still displays the shuffled file to stdout, but this time it deletes the original. I suggest you don't use it.
考虑一个 shell 脚本:
Consider a shell script:
#!/bin/sh
if [[ $# -eq 0 ]]
then
echo "Usage: $0 [file ...]"
exit 1
fi
for i in "$@"
do
perl -MList::Util -e 'print List::Util::shuffle <>' $i > $i.new
if [[ `wc -c $i` -eq `wc -c $i.new` ]]
then
mv $i.new $i
else
echo "Error for file $i!"
fi
done
未经测试,但希望有效.
Untested, but hopefully works.
这篇关于如何使用 Red Hat Linux 上的标准工具随机化文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!