Find and arrange words with same letters from list

I am making a word game and I am wondering how to find and arrange words in a list that have the same letters. In my game, you are presented with 5 letters, and you then have to rearrange the letters tp make a word. So the word could be "acorn", but those 5 letters could also make up "narco" or "racon" which are words in my .txt dictionary (shown on the left).

Example, third line on the right: i.stack.imgur.com/2EVpB.png

I got some assistance at Stackoverflow, but I am wondering how I can actually do this using unix tools like sed/gsed:

Here's the solution I got there:

Any tips or help would be greatly appreciated!

Main tip is to index your master word list with a key of the word's letters sorted to alphabetic order.

wordlist.txt

acnor:acorn
acnor:narco
acnor:racon

The sort the search term to alphabetic order and use grep to search the master word list:

input="acorn"
search=`echo "${input}"|fold -w 1|sort|tr -d '\n'`
grep \^"${search}:" wordlist.txt | awk -F: '{print $2}'

./scriptname
acorn
narco
racon

Thank you for your reply methyl.

Basically I need to find out how to index the master list automatically, because my list is 3000 words long so it would take too long to go through every word manually. Can sed or grep do this?

And I tried running the script, but it doesn't print out anything. I made it search for a word which I knew had anagrams, but it just doesn't do anything. Is it easy to make it write to a new .txt file instead to more easily verify that it works?