Linux Tips and Tricks

Linux Tips and Tricks

Scramble Word Finder – Shell Script

June19

Scramble Word Finder – Shell Script

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

/usr/share/dict/american-english

Now sort each string by character wise and store it in seperate file called words_sorted.txt

1
perl -lane '@chars=split(//,$_); printf("%s%25s\n",$_,lc join (/,/, sort @chars));'  /usr/share/dict/american-english > words_sorted.txt

Now all the words are sorted and the original word is in first column and the sorted value is in second column.

1
2
3
4
5
6
7
8
9
10
11
$ head words_sorted.txt
 
A                        a
A's                      'as
AOL                      alo
AOL's                    'alos
Aachen                   aacehn
Aachen's                 'aacehns
Aaliyah                  aaahily
Aaliyah's                'aaahilsy
Aaron                    aanor

Now write a script to find the scramble words.

Save the below script as scramble.sh

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
 
echo -n "Enter the Scrambled Word : "
read word
 
#sort the characters within the word
sorted_word=$(echo "$word" | grep -o . | sort |tr -d "\n")
 
matched_words=$(awk -v word="$sorted_word" '{if($2~word && length($2) == length(word)){print $1}}' words_sorted.txt)
 
echo "Given Input : $word"
echo "The Matched words are : $matched_words"

Now the run the script and check the output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ./scramble.sh
Enter the Scrambled Word : tleeivisno
Given Input : tleeivisno
The Matched words are : television
 
$ ./scramble.sh           
Enter the Scrambled Word : ifnd
Given Input : ifnd
The Matched words are : find
 
$ ./scramble.sh
Enter the Scrambled Word : srbcalme
Given Input : srbcalme
The Matched words are : clambers
scramble

How to solve the Crossword Puzzle ?

Here is the script

http://www.thelinuxtips.com/2010/12/solve-the-crossword-puzzle/

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories