1# anagram.awk --- An implementation of the anagram-finding algorithm 2# from Jon Bentley's "Programming Pearls," 2nd edition. 3# Addison Wesley, 2000, ISBN 0-201-65788-0. 4# Column 2, Problem C, section 2.8, pp 18-20. 5# 6# This program requires gawk 4.0 or newer. 7# Required gawk-specific features: 8# - True multidimensional arrays 9# - split() with "" as separator splits out individual characters 10# - asort() and asorti() functions 11# 12# See https://savannah.gnu.org/projects/gawk. 13# 14# Arnold Robbins 15# arnold@skeeve.com 16# Public Domain 17# January, 2011 18 19/'s$/ { next } # Skip possessives 20{ 21 key = word2key($1) # Build signature 22 data[key][$1] = $1 # Store word with signature 23} 24# word2key --- split word apart into letters, sort, and join back together 25 26function word2key(word, a, i, n, result) 27{ 28 n = split(word, a, "") 29 asort(a) 30 31 for (i = 1; i <= n; i++) 32 result = result a[i] 33 34 return result 35} 36END { 37 sort = "sort" 38 for (key in data) { 39 # Sort words with same key 40 nwords = asorti(data[key], words) 41 if (nwords == 1) 42 continue 43 44 # And print. Minor glitch: trailing space at end of each line 45 for (j = 1; j <= nwords; j++) 46 printf("%s ", words[j]) | sort 47 print "" | sort 48 } 49 close(sort) 50} 51