Issue
I'm trying to map a fixed set of ASCII characters to a fixed set of Unicode characters. I.e., for each digit 0-9, I want to get the circled digit equivalent.
mapFrom="0123456789"
mapTo="🄋➀➁➂➃➄➅➆➇➈"
today=20221018
#convert to "➁🄋➁➁➀🄋➀➇"
todayWithCircles=$(do_something_here) # <-- what's the "something"?
echo $todayWithCircles
# output: ➁🄋➁➁➀🄋➀➇
Given two fixed strings of equal length, what is the easiest way to map them-- based on their position in the string-- as described?
Solution
A quick and dirty approach using perl
to do the translating:
#!/usr/bin/env bash
mapFrom="0123456789"
mapTo="🄋➀➁➂➃➄➅➆➇➈"
today=20221018
perl -CSD -Mutf8 -pe "tr/$mapFrom/$mapTo/" <<<"$today"
outputs
➁🄋➁➁➀🄋➀➇
(Assuming the script is encoded in UTF-8, of course. -CSD
tells perl to use UTF-8 encoding for standard input, output, and other opened files, -Mutf8
tells it that the script itself (The argument to -e
) is encoded in UTF-8.)
If you have GNU sed available, you can use it instead:
sed "y/$mapFrom/$mapTo/" <<<"$today"
(See this unix stackexchange Q&A for why tr
isn't suitable)
Answered By - Shawn Answer Checked By - Mildred Charles (WPSolving Admin)