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?
A simple and intuitive way would be to use
shuf
.Example:
Assume
words.txt
as:To shuffle the lines, do:
which would throws the shuffled lines to standard output; So, you've to pipe it to an output file like:
One such shuffle run could yield:
If you have Scala installed, here's a one-liner to shuffle the input:
One liner for Python based on scai's answer, but a) takes stdin, b) makes the result repeatable with seed, c) picks out only 200 of all lines.
Simple awk-based function will do the job:
usage:
This should work on almost any UNIX. Tested on Linux, Solaris and HP-UX.
Update:
Note, that leading zeros (
%06d
) andrand()
multiplication makes it to work properly also on systems wheresort
does not understand numbers. It can be sorted via lexicographical order (a.k.a. normal string compare).A one-liner for python:
And for printing just a single random line:
But see this post for the drawbacks of python's
random.shuffle()
. It won't work well with many (more than 2080) elements.In windows You may try this batch file to help you to shuffle your data.txt, The usage of the batch code is
After issuing this command, maclist_temp.txt will contain a randomized list of lines.
Hope this helps.