bash 使用 tr 用多个字符替换一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6355011/
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
Replacing one char with many chars with using tr
提问by thetux4
`echo "a~b" | tr '~' "=="`
This outputs a=b. But i wanted a==b. How can i do this with using tr?
这输出 a=b。但我想要 a==b。我如何使用 tr 来做到这一点?
回答by Prince John Wesley
trjust can translate/delete characters.
tr只能翻译/删除字符。
Try something like this:
尝试这样的事情:
echo "a~b" | sed 's/~/==/g'
回答by dogbane
You can't with tr.
你不能用tr.
Instead, use bash string manipulation ${string/substring/replacement}. Example:
相反,使用 bash 字符串操作${string/substring/replacement}。例子:
str="a~b"
echo ${str/"~"/"=="}
Or use sed:
或使用sed:
echo "a~b" | sed 's/~/==/'
回答by Ignacio Vazquez-Abrams
You can't; tr can only map single characters. Use sed.
你不能;tr 只能映射单个字符。使用 sed。

