summaryrefslogtreecommitdiff
path: root/hangman-helper.sh
blob: d6bf0ef260a71fb22cf2c7571414af89060b2cf9 (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
#!/bin/bash
# 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.
#
# Witht 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