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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomize lines of a file 1

Status
Not open for further replies.

ianicr

IS-IT--Management
Nov 4, 2003
230
GB
Is there a command or script to randomize the lines of a file?

Thanks
 
One way...

awk 'BEGIN {srand}
{do x=rand; while(a[x]!=""); a[x]=$0}
END {for(x in a) print a[x]}
' infile
 
Here's a Korn shell version...
Code:
#!/bin/ksh

typeset -RZ6 KEY

while read RECORD
do
        KEY=${RANDOM}
        print "${KEY}:${RECORD}"
done < file.orig | sort -n | sed 's/^[0-9]*://1' > file.unsorted
...where [tt]file.orig[/tt] and [tt]file.unsorted[/tt] are the input and output files.

Hope this helps.

 
Thanks for both. Used the awk one and it worked great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top