Contact Info

(for those who care)

Instant Gratification   

Fri, 19 Jun 2009

bash Pro-TipTM: Select a Random Line

I can’t count the number of times I’ve wanted a simple random selection from a file. I always get confused by trips down $RANDOM and perl-isms, but no more! I present to you:

    ... | sort -R | head -1

sort -R — sort by random hash of keys.

Developers of sort, you are mad geniuses for hiding “put things in correct order” and “put things in totally incorrect order” in the same place.

16:09 CST | category / entries
permanent link | comments?

Like what you just read? Subscribe to a syndicated feed of my weblog, brought to you by the wonders of RSS.

Thanks for Visiting!