I am trying to increment a number in the middle of the string. Tried many ways but didn't find a solution. Any ideas in shell script Ex:- i have a string sam_2.0_protected_dev_branch. I want to increment the number at the middle of the string. So the output should look like sam_2.0_kumar_dev_branch sam_2.1_kumar_dev_branch sam_2.2_kumar_dev_branch ...
CodePudding user response:
Maybe instead of trying to change the string, divide it in 3 parts. text, number and text. you can increment the number and then make one string with that 3 parts.
Sorry if the terminology isn't right
here is a proper documentation: https://ss64.com/ps/syntax-concat.html
Code :
$version = 1.0
$nameOne = "sam_"
$nametwo = "_protected_dev_branch"
for ($i = 1; $i -lt 5; $i )
{
$fullName = $nameOne $version $nametwo
Write-Host $fullName
$version = $version 0.1
}
Output :
sam_1_protected_dev_branch
sam_1.1_protected_dev_branch
sam_1.2_protected_dev_branch
sam_1.3_protected_dev_branch
there's a way to to force a number to show number of numbers after the comma, but I will let you deal with it
$version = 1
for ($i = 1; $i -lt 5; $i )
{
$fullName = "sam_2." $version "_protected_dev_branch"
Write-Host $fullName
$version = $version 1
}