by js2 · 4y ago · 0 comments
#!/usr/bin/env bash
shopt -s nocasematch
while read -r word; do
if [[ $word =~ ^[abcdefoi]{6,6}$ ]]; then
word=${word//o/0}
word=${word//i/1}
word=${word^^}
printf '#%s\n' "$word"
fi
done < /usr/share/dict/words
This could be collapsed to one line with semicolons. On the macOS 12.6 dictionary I get 59 words.Edit: and in sed which someone just asked me for elsewhere:
sed -n -e '
/^[abcdefoi]\{6,6\}$/I {
s/o/0/g;
s/i/1/g;
s/^/#/;
y/abcdef/ABCDEF/;
p;}' < /usr/share/dict/words sed -n -e 'y/abcdefOoIi/ABCDEF0011/' -e 's/^[A-F01]\{6\}$/#&/p' /usr/share/dict/wordsWhy? The only GNUish bit is the grep -P option, which is unnecessary (-E will do as well).
bash is actually pretty powerful if you don't mind its baroque syntax. Writing it in POSIX would be a bit more challenging. You could use a case statement for the pattern matching, but I'm not sure about the substitution.