How to convert numbers to the first letters of the

2019-06-18 07:02发布

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.

标签: bash shell
5条回答
The star\"
2楼-- · 2019-06-18 07:08
 echo 12345 | tr '[0-9]' '[a-j]'
查看更多
霸刀☆藐视天下
3楼-- · 2019-06-18 07:17
tr 0123456789 abcdefghij < filename
查看更多
成全新的幸福
4楼-- · 2019-06-18 07:17

There's more than one way to do it:

perl -lnaF -e 'print map chr($_+97), @F' file
abcdefghij
查看更多
Deceive 欺骗
5楼-- · 2019-06-18 07:23

With sed's map operator.

sed 'y/12345/hWa!-/' <<< '2313134'
查看更多
不美不萌又怎样
6楼-- · 2019-06-18 07:23

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).

查看更多
登录 后发表回答