I have strings which represent versions (always in the following format):
1.0.0
1.1.0
1.5.0
20.189.0
456874.0.0
The last part of the string will always be .0
.
Now I'm searching for a way in bash how I can replace this with .X
1.0.X
1.1.X
9.5.X
20.189.X
...
How to replace the character in a string with Bash. bash 1min read. To replace one character in a string with another character, we can use the parameter extension in Bash (shell). Here is an example that removes the character a with b in the following string. string="abc" final=$ {string//[a]/b} echo $final. Output:
Let’s have a look at different ways of removing the last characters from a string. To remove the last characters from a string, type the variable name followed by a % symbol and a number of ? symbols equal to the number of characters to be removed. variable="verylongstring" echo $ {variable%??????}
Reverse the string and use REPLACE FIRST OCCURRENCE statement. 2. Or else, search for the offset of last , and then replace that offset using REPLACE SECTION statement. Hope it helps you. Sindhu Pulluru.
Replace the % symbol with the # symbol to remove characters from the starting point. The main idea of this method is to chop from the beginning of the string up to the length - number of characters to remove. This is done by $ {variable_name:starting_index:last_index}
Using bash
manipulation:
str='1.0.0'
echo "${str/%.0/.X}"
1.0.X
or else:
echo "${str%.0}.X"
1.0.X
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