How to shuffle lines using the command line
Sorting lines is easy: everybody knows the sort
command.
But what if you want to do the other way around,
and shuffle instead of sorting?
This perl
one-liner does just that:
... | perl -MList::Util -e 'print List::Util::shuffle <>'
Explanation:
-
-MList::Util
load theList::Util
module (as if doinguse List::Util
inside a Perl script) -
-e '...'
execute Perl command -
print List::Util::shuffle <>
callList::Util::shuffle
for the lines coming from standard input, read by<>
Another way would be sort -R
if your version of sort
supports that (GNU, as opposed to BSD).
In BSD systems you can install coreutils
and try gsort -R
instead.
(For eample on OSX, using MacPorts: sudo port install coreutils
.)
Yet another way is shuf
in GNU systems,
and gshuf
in coreutils
.
Written by Janos Gyerik
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Perl
Authors
janosgyerik
25.11K
Jean-Remy Duboc
12.22K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#