Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

anagrams

Status
Not open for further replies.

typeos

Programmer
Oct 29, 2002
1
GB
Hi

I would really appreciate a hand with this.

I have created one program which will receive a list of words then output each word as its signature (the word sorted alphabetically and lower case applied) the original word will be printed out beside it, as follows:

acijkkps skipjack

I am using the /usr/dict/words file which seems to work fine :)
cat /usr/dict/words | prog1 | sort > result


Ok, now the problem is this... I want to create another program in which the output from the above program will be piped to it. I would then like to accumulate words with the same signature, and output the list if there are more than 1 anagram.

I hope this is easy enough to understand, cheers for any help :)

typeos

 


this realy isn't a C question unless you want to write a C program to do this. this is more a UNIX script question and Tek-tips has a forum devoted to that.

anyway here is the easiest way.

your script | sort | awk '{print $2, $1}' | uniq -1 -d | awk '{print $2, $1}'


The sort puts all the duplicate ANAGRAMS together.

The first Awk reverses the output of your output so that

We can run UNIQ -d on the second field to find all the duplicates

The Second Awk puts the data back in your original format.


I tried to do this is a single AWK script but I keep not printing the last row. I could add more logic to get it printed but the above seems so concise.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top