下僅使用基本的bash命令。普林西是:
A B C
--------
a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
e1 e2 e3
f1 f2 f3
g1 g2 g3
h1 h2 h3
i1 i2 i3
j1 j2 j3
:
代碼
#!/bin/bash
case "$#" in
0) echo "Usage: $0 files....." ; exit 1;;
esac
ORDER="./.rand.$$"
trap "rm -f $ORDER;exit" 1 2
count=$(grep -c '^' "$1")
let odcount=$(($count * 4))
paste -d" " <(od -A n -N $odcount -t u4 /dev/urandom | grep -o '[0-9]*') <(seq -w $count) |\
sort -k1n | cut -d " " -f2 > $ORDER
#if your system has the "shuf" command you can replace the above 3 lines with a simple
#seq -w $count | shuf > $ORDER
for file in "[email protected]"
do
paste -d' ' $ORDER $file | sort -k1n | cut -d' ' -f2- > "$file.rand"
done
echo "the order is in the file $ORDER" # remove this line
#rm -f $ORDER # and uncomment this
# if dont need preserve the order
paste -d " " *.rand #remove this line - it is only for showing test result
從輸入文件中的所有文件
會使A.rand B.rand C.rand
下一個示例內容
g1 g2 g3
e1 e2 e3
b1 b2 b3
c1 c2 c3
f1 f2 f3
j1 j2 j3
d1 d2 d3
h1 h2 h3
i1 i2 i3
a1 a2 a3
實際測試 - genereting 50個文件有25K線
line="Consequatur qui et qui. Mollitia expedita aut excepturi modi. Enim nihil et laboriosam sit a tenetur."
for n in $(seq -w 50)
do
seq -f "$line %g" 25000 >file.$n
done
運行腳本
bash sorter.sh file.??
結果在我的筆記本
real 1m13.404s
user 0m56.127s
sys 0m5.143s
的大問題你有(但米ay還沒有注意到)是如何將一個文件放入預定的順序......特別是如果你不能假設整個文件可以一次存儲在內存中。您可能需要創建「中間」文件(每行具有預定順序的行),然後以ping/pong模式瀏覽這些文件,直到您最終獲得一個文件。 – ErstwhileIII
如果您必須處理「文件不適合內存」,請調整您的問題,我可以爲您提供有關「合併排序」過程的建議。 – ErstwhileIII