Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I randomize the lines in a file using standard tools on Red Hat Linux?

How can I randomize the lines in a file using standard tools on Red Hat Linux?

I don't have the shuf command, so I am looking for something like a perl or awk one-liner that accomplishes the same task.

like image 295
Stuart Woodward Avatar asked May 20 '09 05:05

Stuart Woodward


People also ask

How do you shuffle a line in a text file in Linux?

Using the shuf Command The shuf utility is a member of the GNU Coreutils package. It outputs a random permutation of the input lines. The shuf command will load all input data into memory during the shuffling, and it won't work if the input file is larger than the free memory.

How do you use Shuf?

Ways of using shuf command:Suppose file. txt contains 6 lines, then the shuf command displays the input lines in random order as output. Any number of lines can be randomized by using -n option. This will display any two random lines from the file.


2 Answers

Um, lets not forget

sort --random-sort 
like image 148
Jim T Avatar answered Sep 25 '22 00:09

Jim T


shuf is the best way.

sort -R is painfully slow. I just tried to sort 5GB file. I gave up after 2.5 hours. Then shuf sorted it in a minute.

like image 33
Michal Illich Avatar answered Sep 22 '22 00:09

Michal Illich