Thursday, October 11, 2007

I was screwing around this morning and I needed some random words to test something with. The words needed to be real words, not just random sequences of characters (btw, you can generate a random sequence of 8 characters from the shell using jot -r -c 8 a z | rs -g 0 8). In this case, I decided to simply grab a random word from /usr/share/dict/words.

Hmm, but how do I grab a random word from a file? My solution was to generate a random number in the range [1..n] where n is the number of lines in the file, cat -n the file so that line numbers are printed, grep for the line matching the random number, then print out the second column. It looks like this:

Wow, that cat -n | grep stuff seems roundabout. Oh, and jot does seem useful, but I was surprised that its not installed by default, and is in package 'athena-jot' so I'm concerned that its non-standard? How about this (that includes some bash-isms, I think)

Perhaps grep means something different to you; in which case, I would probably agree ;-) However, saying that you should _never_ cat and then grep is just bad advice. Of course grep takes file names as arguments—most Unix tools do. In this case, I was grepping for the line number that was produced by "cat -n". I wasn't simply using cat to spit out the contents of the file (though I agree that "cat foo | grep bar" is unnecessary because "grep bar foo" does the job with one less process). Grep matches patterns, not line numbers, so the cat -n was producing a pattern that grep could match. So, in this case, grepping the cat is a valid solution. Another good solution is to use the head/tail tricks as others have already pointed out.