我想随机洗牌文本文件的行,并创建一个新文件。该文件可能有几千行。
我如何用cat, awk, cut等做到这一点?
我想随机洗牌文本文件的行,并创建一个新文件。该文件可能有几千行。
我如何用cat, awk, cut等做到这一点?
当前回答
Ruby增值:
ls | ruby -e 'puts STDIN.readlines.shuffle'
其他回答
Ruby增值:
ls | ruby -e 'puts STDIN.readlines.shuffle'
在windows下,您可以尝试此批处理文件来帮助您洗牌data.txt,批处理代码的用法是
C:\> type list.txt | shuffle.bat > maclist_temp.txt
发出这个命令后,maclist_temp.txt将包含一个随机的行列表。
希望这能有所帮助。
这个bash函数有最小的依赖关系(只有sort和bash):
shuf() {
while read -r x;do
echo $RANDOM$'\x1f'$x
done | sort |
while IFS=$'\x1f' read -r x y;do
echo $y
done
}
Perl一行程序是Maxim解决方案的简单版本
perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);' < myfile
尚未被提及:
unsort的util。语法(有点面向播放列表): unsort [-hvrpncmMsz0l][——help][——version][——random][——heuristic] [——identity][——filenames[=profile]][——separator sep][——concatenate] [——合并][——merge-random][——种子整数][——字符串作为][——零] [——linefeed][文件…] Msort可以逐行洗牌,但它通常是多余的: Seq 10 | msort -jq -b -l -n 1 -c r