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
Or get it from MacPorts:
$ sudo port install coreutils
and/or
$ /opt/local//libexec/gnubin/sort --random-sort
Um, lets not forget
sort --random-sort
When I install coreutils with homebrew
brew install coreutils
shuf
becomes available as n
.
FreeBSD has its own random utility:
cat $file | random | ...
It's in /usr/games/random, so if you have not installed games, you are out of luck.
You could consider installing ports like textproc/rand or textproc/msort. These might well be available on Linux and/or Mac OS X, if portability is a concern.
Related to Jim's answer:
My ~/.bashrc
contains the following:
unsort ()
{
LC_ALL=C sort -R "$@"
}
With GNU coreutils's sort, -R
= --random-sort
, which generates a random hash of each line and sorts by it. The randomized hash wouldn't actually be used in some locales in some older (buggy) versions, causing it to return normal sorted output, which is why I set LC_ALL=C
.
Related to Chris's answer:
perl -MList::Util=shuffle -e'print shuffle<>'
is a slightly shorter one-liner. (-Mmodule=a,b,c
is shorthand for -e 'use module qw(a b c);'
.)
The reason giving it a simple -i
doesn't work for shuffling in-place is because Perl expects that the print
happens in the same loop the file is being read, and print shuffle <>
doesn't output until after all input files have been read and closed.
As a shorter workaround,
perl -MList::Util=shuffle -i -ne'BEGIN{undef$/}print shuffle split/^/m'
will shuffle files in-place. (-n
means "wrap the code in a while (<>) {...}
loop; BEGIN{undef$/}
makes Perl operate on files-at-a-time instead of lines-at-a-time, and split/^/m
is needed because $_=<>
has been implicitly done with an entire file instead of lines.)