The Computer Oracle

Equivalent of gnu `sort -R` on OSX?

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams

--

Chapters
00:00 Question
00:24 Accepted answer (Score 53)
01:28 Answer 2 (Score 8)
01:50 Answer 3 (Score 0)
02:15 Answer 4 (Score 0)
02:48 Thank you

--

Full question
https://superuser.com/questions/334450/e...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #sorting

#avk47



ACCEPTED ANSWER

Score 53


If you want, you can install GNU sort through GNU's coreutils package over Homebrew, which is a package manager for OS X.

Running this would install Homebrew.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then just follow the installation instructions. When Homebrew is installed, run

brew install coreutils

This will install GNU sort as gsort, so you can use it like sort on any GNU Linux.


Alternatively, have a look at these Stack Overflow questions, which mention a couple of methods:

How can I randomize the lines in a file using a standard tools on Redhat Linux
How can I shuffle the lines of a text file in Unix command line?

Or take a look at this commandlinefu.com page:

Randomize lines (opposite of | sort)




ANSWER 2

Score 8


On OS X, if you don't want to install homebrew (but you really should), you could use perl or ruby:

perl -MList::Util -e 'print List::Util::shuffle <>'

or

ruby -e 'puts STDIN.readlines.shuffle'



ANSWER 3

Score 0


Use shuf or sort from coreutils package, but then you've to add /usr/local/bin to your PATH in ~/.bashrc file, for example:

export PATH=/usr/local/sbin:/usr/local/bin:$PATH

Alternatively you can try ex:

ex -s +"%s/^/\=reltimestr(reltime())[-2:].' '" +"sort n" +"%s/^\S* //" +%p -cq! /dev/stdin

Source: How to shuffle a list in vim?




ANSWER 4

Score 0


You could install the rl command via homebrew (brew install randomize-lines).

It is quite fast and has options to limit the number of returned items, as well as specify the delimiter (space instead of line feed, for instance).

(See also https://stackoverflow.com/a/42056195/43615.)