回答:
合理的な新しいLinux / UNIXディストリビューションを使用しているのなら、 sort が付属しています -R 行をソートするのではなく、ランダムにするフラグです。これを使って、このワンライナーソリューションを作成できます。
awk '{printf("%s%s",$0,(NR%4==0)?"\n":"\0")}' file.txt | sort -R | tr "\0" "\n" > sorted.txt
まず、 awk 置き換えて4行ごとにグループ化する \n と \0。それから次のようにして行をシャッフルします。 sort -R そして最後に改行を元に戻します。 tr。
"\n 閉じられていないはずです "\n"。私はそのような小さな編集をすることができないので、それをここに置くだけです。
printf("%s%c",$0,(NR%4==0)?"\n":0)。 trコマンドを変更する必要はありませんでした。
これはPythonです。誰かがPerlの回答も投稿するでしょう。 ;-)
#!/usr/bin/python
import random
#Change these to the desired files
infile = "/path/to/input/file"
outfile = "/path/to/output/file"
fh = file(infile)
contents = fh.readlines()
fh.close()
chunked = [contents[i:i+4] for i in xrange(0, len(contents), 4)]
random.shuffle(chunked)
fh = file(outfile, 'w')
for chunk in chunked:
for line in chunk:
fh.write(line)
fh.close()
誰かがおそらくこれを改善することができるのでIANAプログラマー。