Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numbers to the first letters of the alphabet?

Tags:

bash

shell

I have a file with content like

12345

I need to convert this kind of strings like this:

"0"->"a"
"1"->"b"
...
"9"->"j"

So, 12345 should result in abcde. I want to achieve this via the shell (bash). What is the best way to do this?

Thanks.

like image 305
vinnitu Avatar asked Feb 17 '11 15:02

vinnitu


People also ask

How do you translate a number into a name?

Using a calculator or a pencil and paper, add together every single digit from your name. If your name has 20 letters, you will be adding together 20 individual numbers. You will end up with a 2-digit sum after adding everything together. For example, BATMAN is numbers 2+1+2+4+1+5, which equals 15.


2 Answers

In any shell, you could use:

echo "$string" | tr 0123456789 abcdefghij

Or, in Bash and without a pipe:

tr 0123456789 abcdefghij <<< "$string"

(where the double quotes might not be necessary, but I'd use them to be sure).

like image 102
Jonathan Leffler Avatar answered Nov 05 '22 20:11

Jonathan Leffler


 echo 12345 | tr '[0-9]' '[a-j]'
like image 29
akira Avatar answered Nov 05 '22 21:11

akira