乱序排列文件中的所有行(转)[@more@]
[root@www root]# more test.dat
1 --- I
2 --- L
3 --- O
4 --- V
5 --- E
6 --- Y
7 --- O
8 --- U
9 --- !
10 --- !
[root@www root]# more r.py
import sys,random
lines = sys.stdin.readlines()
olines=[]
while lines:
olines.append(lines.pop(random.randrange(len(lines))))
sys.stdout.write( "".join(olines))
[root@www root]# cat test.dat | python r.py
5 --- E
3 --- O
6 --- Y
10 --- !
8 --- U
1 --- I
7 --- O
2 --- L
9 --- !
4 ---
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8225414/viewspace-945961/,如需转载,请注明出处,否则将追究法律责任。