The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Get A Random Line From A Text File On The Mac Command Line

TODO: Pull subtitle into page object

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:

code_start_default_section code_end_default_section

### Details

- The `sort -R` creates a randomized set of lines from the file - Then `head -1` cuts it down to just the first line. It works great

### Notes:

- 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. - The `sort` and `head` commands 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.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Get A Random Line From A Text File On The Mac Command Line

-- p

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.

-- p

To pull a random quote/line, I'm using:

-- code/
-- bash

sort -R quotes.txt | head -1

-- /code

-- p

### Details

-- p

- The `sort -R` creates a randomized set of lines from the file
- Then `head -1` cuts it down to just the first line.  It works great

-- p

### Notes:

-- p

- 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.
- The `sort` and `head` commands 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.


-- categories
-- Command Line

-- metadata
-- date: 2022-04-19 16:07:15
-- id: 281zn1oj
-- status: published
-- type: post
-- SCRUBBED_NEO: false
-- site: aws