Hacker News new | ask | show | jobs
by TheDong 2377 days ago
Your cute one-liner for 4 words:

sort --random-sort /usr/share/dict/words | head -n 4 | tr -d '\n'

You may wish to omit words that have "'" characters, in which case you may throw in a grep -v "'" after the sort.

1 comments

Thank you. I think it's a testament to the power of unix that two different people responded with solutions using different commands. I should have thought of sort, it feels natural in retrospect. Am I right in believing bash pipes are lazy enough that that won't require sorting the whole file, just pulling out four random lines?

As a side note, I'm guessing grep had -v before it became a standard for verbose? That's one of the hardest parts: it seems open source tools never change UI to help new users at the expense of old ones having to change workflows. A valid way of doing it, and as someone who's never contributed to anything open source I have no place to criticize. Just saying makes it harder. (And I'm realizing I haven't used any closed source software from a comprable time, so I have no idea if this is FOSS-specific)

I don't think the pipe could know that head will have the -n 4. In what context is -v standard for verbose?

edit: This stack exchange seems to relevant https://stackoverflow.com/questions/45326901/lazy-non-buffer...

I don't think grep was originally open source. FWIW, the nemonic for -v on grep is inVert. I'm so happy that --long_options are a thing nowadays, much more readable.

I'm pretty sure that head pulls from up in the pipeline line-by-line. Interesting to know the mnemonic. Many modern command-line utilities use -v for verbose logging, in my (limited) experience.