| 1 | #!/bin/bash |
| 2 | # cw-solver.sh |
| 3 | # This is actually a wrapper around a one-liner (line 46). |
| 4 | |
| 5 | # Crossword puzzle and anagramming word game solver. |
| 6 | # You know *some* of the letters in the word you're looking for, |
| 7 | #+ so you need a list of all valid words |
| 8 | #+ with the known letters in given positions. |
| 9 | # For example: w...i....n |
| 10 | # 1???5????10 |
| 11 | # w in position 1, 3 unknowns, i in the 5th, 4 unknowns, n at the end. |
| 12 | # (See comments at end of script.) |
| 13 | |
| 14 | |
| 15 | E_NOPATT=71 |
| 16 | DICT=/usr/share/dict/word.lst |
| 17 | # ^^^^^^^^ Looks for word list here. |
| 18 | # ASCII word list, one word per line. |
| 19 | # If you happen to need an appropriate list, |
| 20 | #+ download the author's "yawl" word list package. |
| 21 | # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz |
| 22 | # or |
| 23 | # http://bash.deta.in/yawl-0.3.2.tar.gz |
| 24 | |
| 25 | |
| 26 | if [ -z "$1" ] # If no word pattern specified |
| 27 | then #+ as a command-line argument . . . |
| 28 | echo #+ . . . then . . . |
| 29 | echo "Usage:" #+ Usage message. |
| 30 | echo |
| 31 | echo ""$0" \"pattern,\"" |
| 32 | echo "where \"pattern\" is in the form" |
| 33 | echo "xxx..x.x..." |
| 34 | echo |
| 35 | echo "The x's represent known letters," |
| 36 | echo "and the periods are unknown letters (blanks)." |
| 37 | echo "Letters and periods can be in any position." |
| 38 | echo "For example, try: sh cw-solver.sh w...i....n" |
| 39 | echo |
| 40 | exit $E_NOPATT |
| 41 | fi |
| 42 | |
| 43 | echo |
| 44 | # =============================================== |
| 45 | # This is where all the work gets done. |
| 46 | grep ^"$1"$ "$DICT" # Yes, only one line! |
| 47 | # | | |
| 48 | # ^ is start-of-word regex anchor. |
| 49 | # $ is end-of-word regex anchor. |
| 50 | |
| 51 | # From _Stupid Grep Tricks_, vol. 1, |
| 52 | #+ a book the ABS Guide author may yet get around |
| 53 | #+ to writing . . . one of these days . . . |
| 54 | # =============================================== |
| 55 | echo |
| 56 | |
| 57 | |
| 58 | exit $? # Script terminates here. |
| 59 | # If there are too many words generated, |
| 60 | #+ redirect the output to a file. |
| 61 | |
| 62 | $ sh cw-solver.sh w...i....n |
| 63 | |
| 64 | wellington |
| 65 | workingman |
| 66 | workingmen |