Linux Tips and Tricks

Linux Tips and Tricks

Print Random word using the Dictionary file – AWK

September16

Most of the Debian system has the below dictionary file ( which is the pure ASCII words )

/usr/share/dict/american-english

we have a built-in variable called RANDOM in bash and ksh shells. Using that, we can print random words from the above given dictionary file.

example of RANDOM variable

1
2
3
4
$ echo $RANDOM
30728
$ echo $RANDOM
24228

Using the below code, we can print some random word from the dictionary file.

1
2
3
4
5
6
7
8
9
10
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Jeannie's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
authorization
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Yiddish's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Constantine's
$ awk -v lineno="$RANDOM" 'lineno==NR{print;exit}' /usr/share/dict/american-english
Miro

In the above awk command, lineno is a variable which holds the RANDOM number, then we are comparing with processing line number of awk. If both number matches, we are printing that word.
exit is used to avoid processing/reading the remaining lines.

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories