Posted in Linux, news, Security, Seguranca, Unix, volodia sascho, volodia sascho junior, tagged Algorithms, Cat, Games, Grep, Pseudorandom Numbers, Recreation, Ubuntu, Words (Unix) on August 26, 2011 |
Leave a Comment »
I was screwing around this morning and I needed some random words to test something with. The words needed to bereal 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:
$ n=$(cat /usr/share/dict/words | wc -l)
$ cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2
idic
$ cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2
goldentop
$ cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2
Hamitism
$ cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2
accumulativeness
$ cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2
ratihabition
Read Full Post »