Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the last char of a string in bash with another char

Tags:

bash

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
...
like image 264
DenCowboy Avatar asked Feb 12 '18 10:02

DenCowboy


People also ask

How to replace the character in a string with Bash?

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:

How to remove the last characters from a string in Python?

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%??????}

How to change the offset of last part of a string?

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.

How to remove characters from the starting point of a string?

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}


1 Answers

Using bash manipulation:

str='1.0.0'

echo "${str/%.0/.X}"
1.0.X

or else:

echo "${str%.0}.X"
1.0.X
like image 83
anubhava Avatar answered Oct 17 '22 02:10

anubhava