summaryrefslogtreecommitdiff
path: root/hangman-helper.sh
blob: 1e659695869d660751b1dd43fb25e3dacb4fa09c (plain)
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
#!/bin/bash
# Copyright (C) 2011, 2013-2014 Luke Shumaker <lukeshu@sbcglobal.net>

# Usage: hangman-helper word not [-l]
# <word> is the known word, with unknown characters replaced with '.'.
# <not> is a sequence of the characters that are known to not be in the word.
#
# Without the `-l` flag, it produces a list of possible completions.
#
# With the `-l` flag, it produces a list of letters,
# sorted by the number of times they appear in the normal list

word=$1
not=$2
flag=$3

temp="$(mktemp --tmpdir "${0##*/}.XXXXXXXXXX")"
trap "rm -f -- $(printf '%q' "$temp")" EXIT

grep -ix "$word" /usr/share/dict/words | grep -iv "['$not]" > "$temp"

if [[ "$flag" = '-l' ]]; then
	cat "$temp" | tr 'A-Z' 'a-z' | sed 's/\(.\)/\1\n/'g | sort | uniq -c | sort -n
else
	cat "$temp"
fi