6 ms·
The gist is rather a pipeline of Unix commands with no bash necessarily involved. Here it is in shellcheck-compliant 100% bash: #!/usr/bin/env bash sho
by js2 4y ago
The gist is rather a pipeline of Unix commands with no bash necessarily involved. Here it is in shellcheck-compliant 100% bash:
#!/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
- version_five 4y agoThanks for this. I'd probably call the original the GNU coreutils version. The linked github also has a sed-only version in the comments. It's instructive to see the different versions.
- js2 4y agoI just added a sed version as well. I'll have to click through and see how closely it resembles what's in the gist. 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.
- kps 4y ago> I'd probably call the original the GNU coreutils version. Why? The only GNUish bit is the grep -P option, which is unnecessary (-E will do as well).
- version_five 4y agoI would have considered tr to be part of gnu coreutils, awk, not necessarily but the default on a mac is gawk I believe
- kragen 4y agotr predates GNU by about a decade.
- kps 4y agosed -n -e 'y/abcdefOoIi/ABCDEF0011/' -e 's/^[A-F01]\{6\}$/#&/p' /usr/share/dict/words
- js2 4y agoI’d golf with you but I think you got a hole in one there. I didn’t spend any time thinking about how to make the sed more compact. I sorta just translated what I’d already written in bash.