Get A Random Line From A Text File On The Mac Command Line
I'm working on a little command to display a random quote every time I open a new terminal window. I setup a file with a bunch of quotes. Each one is on its own line.
To pull a random quote/line, I'm using:
sort -R quotes.txt | head -1
sort -Rcreates a randomized set of lines from the file
head -1cuts it down to just the first line. It works great
- Lots of notes on the web say to use
shuf -n 1, but that's not installed by default on my machine running macOS 12.x Monterey.
headcommands were available on my machine without having to install anything. That said, it's possible that something else that I installed with homebrew also installed them. Your milage may vary.