1

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?

1
  • There are a number of strategies for mapping to multi-byte keys in the post Casing arrow keys in bash though they are usually mapping the reverse of what you need. However, if you have the unicode keycode for ➀➁➂➃➄➅➆➇➈, you should be able to do the reverse map the same way using case. Commented Oct 19, 2022 at 2:33

2 Answers 2

5

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I am partial to the sed approach. Thanks for this!
1

Here's a bash only, pure way of doing this.

You loop over each character of the string, and replace it with a rounded character

#!/usr/bin/env bash

mapTo="🄋➀➁➂➃➄➅➆➇➈"
today=0987654321

for ((i = 0; i < ${#today}; i++)); do
  echo -n "${mapTo:${today:$i:1}:1}"
done

result:

🄋➈➇➆➅➄➃➂➁➀

1 Comment

Thank you for this. The sed approach will work a little better for me than looping, but this is a great solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.