`echo "a~b" | tr '~' "=="`
This outputs a=b. But i wanted a==b. How can i do this with using tr?
`tr` command can be used with -c option to replace those characters with the second character that don't match with the first character value. In the following example, the `tr` command is used to search those characters in the string 'bash' that don't match with the character 'b' and replace them with 'a'.
Translate Strings With the tr Command in Linux Bash The tr command means to translate. It is used to delete, translate and squeeze characters. It takes standard input, processes it, and writes it to standard output.
string1 and string2 are considered to be sets of characters. In its simplest form, tr translates each character in string1 into the character at the corresponding position in string2.
The -d ( --delete ) option tells tr to delete characters specified in SET1.
tr
just can translate/delete characters.
Try something like this:
echo "a~b" | sed 's/~/==/g'
You can't with tr
.
Instead, use bash string manipulation ${string/substring/replacement}
. Example:
str="a~b"
echo ${str/"~"/"=="}
Or use sed
:
echo "a~b" | sed 's/~/==/'
You can't; tr can only map single characters. Use sed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With