Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shuffling the lines 1

Status
Not open for further replies.

tivona

Technical User
Oct 23, 2010
6
0
0
HK
Dear all,

Shuffling the lines

I would like to perform the following tasks using awk. I would be grateful for any advices you might have.

1. Shuffle the lines by field 1 (groups 111, 112). That is, within each group shuffling is undertaken independently.

2. After that print out the first 5 lines by groups.

3. Repeat steps 1 to 2 say 100 times and print to file1.txt, file2.txt,... file100.txt.

The number of groups is large in my case.

Input

111 1032192
111 2323476
111 1698881
111 2451712
111 2013780
111 888105
112 2331004
112 1886376
112 1189765
112 1877267
112 1772972
112 574631

Output

1 st time, the output could be

111 2323476
111 1698881
111 2451712
111 2013780
111 888105
112 1886376
112 1189765
112 1772972
112 574631
112 2331004

2nd time, the output could be
111 2451712
111 2013780
111 1698881
111 2323476
111 888105
112 1877267
112 1772972
112 2331004
112 1886376
112 574631


Thank you very much.

Cheers,

T
 

Try this:
Code:
gawk '
function roll(n) {return  1 + int(rand() * n) }
{g[$1]= g[$1]" "$2; }
END {srand();
 for(i in g){
  n=split(g[i], e);
  for(k=1;k<=n;++k)e[k]=roll(31416)"|"e[k];
  asort(e);
  for(k in e){split(e[k],r,"|"); print i,r[2]}
 }
}' MyFile.txt
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Dear LKBrwnDBA,

Thank you very much for your efforts.
After posting the query, I got to know the command "shuf" which is quite handy and does not require to set up the seed. But yours is more instructive in that one can learn more about programming from reading the codes.

Cheers
 

Weird, never heard of "shuf", not in any of my awk manuals...
[noevil]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top