如何使用Red Hat Linux上的标准工具随机分配文件中的行? [英] How can I randomize the lines in a file using standard tools on Red Hat Linux?

查看:109
本文介绍了如何使用Red Hat Linux上的标准工具随机分配文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我没有使用 shuf

code>命令,所以我正在寻找类似于 perl awk 相同的任务。

解决方案

一个Perl一线您可以得到!

  perl -MList :: Util -e'print List :: Util :: shuffle<>'

它使用一个模块,但该模块是Perl代码分发的一部分。如果这还不够,你可以考虑自己滚动。



我尝试使用这个与 -i (编辑就地)让它编辑文件。该文件表明它应该有效,但是它不行。它仍然显示洗牌文件到stdout,但这次它删除原来的。我建议你不要使用它。



考虑一个shell脚本:

 #/ bin / sh 

if [[$#-eq 0]]
then
echo用法:$ 0 [file ...]
exit 1
fi

为$ @
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文件$ i的错误!
fi
done

未经验证,但希望可以工作。


How can I randomize the lines in a file using standard tools on Red Hat Linux?

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.

解决方案

And a Perl one-liner you get!

perl -MList::Util -e 'print List::Util::shuffle <>'

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 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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆