Chinaunix is indeed a place where shell experts gather. As long as you want to get a question, you can basically find the answer there. R2007 provides a clever method. The $ random variable of shell is used to add a random row number to each row of the original file and sort it according to the random row number, filter out the temporary row number, and the new file obtained after the operation is equivalent to being randomly "washed" once:
While read I; do echo "$ I $ random"; done <file | sort-K2n | cut-d ""-F1
Of course, if the content in each line of your source file is complex, you must rewrite this Code. However, as long as you know the key processing skills, the remaining problems are not difficult to solve.
Another random File Sorting code analysis from Su Rongrong using awk to achieve shuffling effect (originally posted here, and a subsequent discussion on this post, if you have not logged on to the account, you can go here to view the excellent area article:
--------------------------------------------------------------------
There is actually a good shell solution for shuffling. Here we also provide three awk-based methods. If there are any errors, please leave it blank.
Method 1:Poor lifting
Similar to the exhaustive method, a hash is constructed to record the number of times rows appear in printed rows. If more than once rows appear, no processing is performed. This prevents repeated rows, however, the disadvantage is that the system overhead is increased.
Awk-v n = 'sed-n' $ = 'data''
Begin {
FS = "/N ";
Rs = ""
}
{
Srand ();
While (T! = N ){
X = int (N * rand () + 1 );
A [x] ++;
If (A [x] = 1)
{
Print $ X; t ++
}
}
}
'Data
Method 2:Transform
The array subscript conversion method stores the content of each row in an array and exchanges the content of the array through the conversion of the array bottom object, which is more efficient than method 1.
#! /Usr/awk
Begin {
Srand ();
}
{
B [Nr] = $0;
}
End {
C (B, NR );
For (X in B)
{
Print B [x];
}}
Function C (ARR, Len, I, j, t, x ){
For (X in ARR)
{
I = int (LEN * rand () + 1;
J = int (LEN * rand () + 1;
T = arr [I];
Arr [I] = arr [J];
Arr [J] = T;
}
}
Method 3:Hash
The best of the three methods.
By using the hash feature in awk (for details, see 7.x in info gawk), you only need to construct a random, non-repeating hash function, because the linenumber of each line in a file is unique, so use:
Random Number + linenumber of each line ------ corresponding ------> content of that line
It is a constructed random function.
Thus there are:
Awk 'in in {srand ()} {B [rand () Nr] = $0} end {for (X in B) print B [x]} 'Data
In fact, you don't have to worry too much about memory usage. You can perform a test:
Test environment:
PM 1.4 GHz CPU, 40 Gb hard disk, memory m laptop
SuSE 9.3 GNU bash version 3.00.16 GNU awk 3.1.4
Generate a random file with hundreds of thousands of lines, about 38 m:
OD/dev/urandom | dd COUNT = 75000> data
For example:
Time used for shuffling:
Time awk-v n = 'sed-n' $ = 'data''
Begin {
FS = "/N ";
Rs = ""
}
{
Srand ();
While (T! = N ){
X = int (N * rand () + 1 );
A [x] ++;
If (A [x] = 1)
{
Print $ X; t ++
}
}
}
'Data
Result (File Content omitted ):
Real 3m41. 864 s
User 0m34. 224 s
Sys 0m2. 102 S
Therefore, the efficiency is barely acceptable.
Test Method 2:
Time awk-F awkfile datafile
Result (File Content omitted ):
Real 2m26. 487 s
User 0m7. 044 s
Sys 0m1. 371 s
The efficiency is significantly better than the first one.
Next we will examine the efficiency of method 3:
Time awk 'in in {srand ()} {B [rand () Nr] = $0} end {for (X in B) print B [x]} 'Data
Result (File Content omitted ):
Real 0m49. 195 s
User 0m5. 318 s
Sys 0m1. 301 s
It is quite good for a 38 M file.
--------------------------------------------------------------------
With a Python version out of order code written by flyfly:
# Coding: gb2312
Import sys
Import random
Def usage ():
Print "Usage: Program srcfilename dstfilename"
Global filename
Filename = ""
Try:
Filename = SYS. argv [1]
Except t:
Usage ()
Raise ()
# Open the PhoneBook File
F = open (filename, 'R ')
Phonebook = f. readlines ()
Print phonebook
F. Close ()
# Write to file randomly
Try:
Filename = SYS. argv [2]
Except t:
Usage ()
Raise ()
F = open (filename, 'w ')
Random. Shuffle (phonebook)
F. writelines (phonebook)
F. Close ()