Apple - Install shuf on OS X?

You can install coreutils with brew install coreutils.

shuf will be linked as gshuf. Read the caveats when you install coreutils.


Yet another solution is to learn about the tools supplied by the vendor. Certainly you could chain jot, paste, sort, cut, head and get the same results.

jot -r "$(wc -l FILE)" 1 |
paste - FILE |
sort -n |
cut -f 2- |
head -n 10
  • jot produces a random number from 1 to the number of lines in FILE for each line
  • paste pastes the random number to each line in FILE
  • sort sorts numeric each line
  • cut removes the random number from each line
  • head outputs the first 10 lines

You can install coreutils with Macports as

sudo port install coreutils

This will put GNU core utils in /opt/local/bin with a g prepended

e.g. gshuf

More details on the package coreutils.

Tags:

Command Line