-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.awk
39 lines (32 loc) · 920 Bytes
/
anagram.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# anagram.awk --- An implementation of the anagram-finding algorithm.
# Implementation done by Arnold Robbins (in Effective awk Programming)
# on the algorithm from Jon Bentley's 'Programming Pearls'.
# usage example:
# $ awk -f anagram.awk /usr/share/dict/words | grep '^b'
/'s$/ { next } # Skip possessives
function word2key(word, a, i, n, result)
{
n = split(word, a, "")
asort(a)
for (i = 1; i <= n; i++)
result = result a[i]
return result
}
{
key = word2key($1) # Build signature
data[key][$1] = $1 # Store word with signature
}
END {
sort = "sort"
for (key in data) {
# Sort words with same key
nwords = asorti(data[key], words)
if (nwords == 1)
continue
# And print. Minor glitch: trailing space at end of each line
for (j = 1; j <= nwords; j++)
printf("%s ", words[j]) | sort
print "" | sort
}
close(sort)
}