Bash one-liner to produce a list of HEX color codes that read like English words
gist.github.com
gist.github.com
Some of them are pretty #bad (#011 doesn't really look much like "oil") and some, though they read quite well, correspond to awful colors; you might even say, #faeca1 colors. Still, I've made my #bed, #0dd as it may be; now I must #11e in it. I think I've #fed you enough #babb1e for today.
#!/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/wordsbash 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.
Why? The only GNUish bit is the grep -P option, which is unnecessary (-E will do as well).
sed -n -e 'y/abcdefOoIi/ABCDEF0011/' -e 's/^[A-F01]\{6\}$/#&/p' /usr/share/dict/wordsThis snippet demonstrates how a number of small tools, each doing its narrow job, strung together via the most trivial interface, produces a non-trivial result.
This composability is still unreachable to the vast majority of GUI tools.
I'm not convinced that it's easier to understand in Python (even though I simplified it a bit, in part because one piece of the Python 3 braindamage was moving string.maketrans to bytes):
import re
def main(words):
for word in words:
word = word.strip().upper()
if re.compile(r'[A-FOI]{6}$').match(word):
print('#' + word.replace('I', '1').replace('O', '0'))
if __name__ == '__main__':
main(open('/usr/share/dict/words'))
I think the shell version is clearly better for interactive improvisation, though. import re
is_hex_like = re.compile(r"^[a-foi]{6}$", re.I).search
for word in filter(is_hex_like, open("/usr/share/dict/words")):
hexword = word.upper().replace("O", "0").replace("I", "1").rstrip()
print(f"#{hexword}") import re
wordlist = open("/usr/share/dict/words").read()
for word in re.findall(r"^[a-foi]{6}$", wordlist, re.IGNORECASE | re.MULTILINE):
hexword = word.upper().replace("O", "0").replace("I", "1")
print(f"#{hexword}")1. Stdin - just iterate through sys.stdin
2. Stdout - regular printing will go there
3. Stderr - print errors here eg with print(…, file=sys.stderr)
And then beyond that as long as your script gets invoked by the interpreter (Ie #!/usr/bin/env python) everything will “just work”.
Not saying it’s hard but also it’s not 100% covered by what you said.
Generally, Python does the right thing by default for scripting use: line buffered, system encoding, EOF handled naturally by the iterator protocol.
[1] https://datascienceatthecommandline.com/2e/chapter-4-creatin...
Can't do computing without that!! :-)
I always have a little giggle.
It's in the US. Here's the census data to discover many occurrences of "1337"
https://www.census.gov/data/tables/time-series/demo/popest/2...
FWIW the town I'm talking about has a different population listed there, a little bit short. The road sign still says 1337, though, as of Thursday.
And since there's no real place to mention this elsewhere, there's a HTML color bot on fediverse (botsin.space) that periodically posts two colors, that work as compliments as foreground and background, and vice versa. I haven't seen it in a while, but our little instance has gotten popular so the feed rate is up near a few hundred posts an hour to sift through.
This is also an 8 character string, which I had wrongly inferred from usage in existing code to be restricted to certain APIs, but I looked it up and it’s evidently part of CSS Color Module Level 4 and has wide browser support. The one-liner could trivially be expanded to support 8-character codes. Not sure how trivial multiple words would be, my gut says “reasonably so but won’t feel quite so reasonable on one line”. Alas I’m on mobile so I’m not gonna try it right now.
That entirely depends on the Linux distro.
The git.kernel.org repository
Slackware
Debian
Unbuntu
Gentoo
Arch initramfs
Alpine
Tiny Core
OpenWRT
Any other distrib that uses Busybox
Android
What the OP fails to mention is that this shell one-liner (cf. "Bash one-liner"), as written, requires GNU grep, thanks to "-P".BusyBox grep does not have a "-P" option.
In the case of Android, Google uses NetBSD userland programs, e.g., grep, which also does not include PCRE, i.e., "-P".
https://coral.googlesource.com/android-core/+/3458bb6ce1d3e7...
https://git.kernel.org/pub/scm/utils/dash/dash.git/
curl -O https://mirror.rackspace.com/archlinux/iso/2022.10.01/arch/boot/x86_64/initramfs-linux.img
xz -dc < initramfs-linux.img|cpio -t|grep -m1 usr/bin/ashPerhaps this is why use of regex is so controversial amongst a majority of "professional" programmers. They are trying to use PCRE for every pattern matching task, i.e, even ones where it is not necessary, whether it is within their programing language or with command-line utilities. This "Bash one-liner" is a simple example.
I have reviewed a number of books written about regular expressions and for the most part^1 they focus only on regex as implemented in popular programming languages. That almost invariably is PCRE or some form of PCRE-like pattern matching. There is little distinction, let alone acknowledgment, between PCRE/PCRE-like patterns and anything simpler.
Not being a "professional" programmer, I use regex everyday but I never (intentionally) use PCRE.^2 Too complicated for my tastes, not to mention slow if using backtracking.
1. I recall one older book that did include an incomplete table attempting to show which type of regex was used by various UNIX utilities in addition to what regex was used by popular programming languages of the day.
2. For programs that optionally link to a PCRE library, I re-compile without them without it.
mawk 'BEGIN{b = "[abcdefois]"; l = "[a-z]"; W = "^" b l l l l l "$"}; $0 ~ W {print "#" toupper($0);}' /usr/share/dict/words gawk 'BEGIN {IGNORECASE=1} ((length($1) == 6) && /^[a-fois]+$/) {gsub(/o/,0);gsub(/i/,1);gsub(/s/,5); print toupper("#"$1)}' /usr/share/dict/words
(caveat: it does not filter out duplicates) sed -E -e '/^[a-fio]{6}$/!d; y/abcdefioIO/ABCDEF1010/; s/^/#/' /usr/share/dict/wordsHere's a fixed version that also handles S/5:
sed -E -e '/^[A-FIOSa-fios]{6}$/!d; y/abcdefiosIOS/ABCDEF105105/; s/^/#/' /usr/share/dict/words"...We used to go to lunch at a place called St Michael’s Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line, it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after “CAFE” (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn’t seem terribly important or destined to go anywhere but the trash can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD – it was eventually replaced by RMI...."
- James Gosling
Now I will never be able to see without thinking of this story: https://aphyr.com/posts/341-hexing-the-technical-interview
https://codepen.io/srcreigh/pen/QWrrgdx
Code thanks to gabrielsroka on the Github thread
sed -n -e 'y/abcdefOoIi/ABCDEF0011/' -e '/^[A-F01]\{6\}$/p' /usr/share/dict/words | while read c; do printf '\033[38;2;%d;%d;%dm#%s\033[0m\n' $((0x${c:0:2})) $((0x${c:2:2})) $((0x${c:4})) $c; done0123456789ABCDEF
isn't it?
The 3 for E in 1337 speak was on numerical calculators that didn't display letters.