bash 如何将数字转换为字母表的第一个字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5031176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert numbers to the first letters of the alphabet?
提问by vinnitu
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, 12345should result in abcde. I want to achieve this via the shell (bash). What is the best way to do this?
所以,12345应该导致abcde. 我想通过 shell (bash) 来实现这一点。做这个的最好方式是什么?
Thanks.
谢谢。
回答by Jonathan Leffler
In any shell, you could use:
在任何外壳中,您都可以使用:
echo "$string" | tr 0123456789 abcdefghij
Or, in Bash and without a pipe:
或者,在没有管道的 Bash 中:
tr 0123456789 abcdefghij <<< "$string"
(where the double quotes might not be necessary, but I'd use them to be sure).
(双引号可能不是必需的,但我会使用它们来确定)。
回答by akira
echo 12345 | tr '[0-9]' '[a-j]'
回答by Ignacio Vazquez-Abrams
With sed's map operator.
使用 sed 的 map 操作符。
sed 'y/12345/hWa!-/' <<< '2313134'
回答by frankc
tr 0123456789 abcdefghij < filename
回答by Eugene Yarmash
There's more than one way to do it:
有不止一种方法可以做到:
perl -lnaF -e 'print map chr($_+97), @F' file
abcdefghij

