I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines.
How can I do that with cat
, awk
, cut
, etc?
I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines.
How can I do that with cat
, awk
, cut
, etc?
We have a package to do the very job:
Example:
Create an ordered list of numbers, and save it to 1000.txt:
to shuffle it, simply use
Perl one-liner would be a simple version of Maxim's solution
Here is a first try that's easy on the coder but hard on the CPU which prepends a random number to each line, sorts them and then strips the random number from each line. In effect, the lines are sorted randomly:
If like me you came here to look for an alternate to
shuf
for macOS then userandomize-lines
.Install
randomize-lines
(homebrew) package, which has anrl
command which has similar functionality toshuf
.brew install randomize-lines
Another
awk
variant:Ruby FTW: