Replacing specific numbers in list of numbers

Hello All!

I’m a bit new to powershell and I’m having trouble and need some guidance on how to accomplish something.

I want to be able to remove specific numbers in a string of numbers.

Say I have this: 608,8,154,266

I’m passing this in and want to remove the 8 and 154 as well as the ,

Here’s my If:

if (($list -eq “8”) -or ($list -eq “266”)){
$newlist = $list -Replace “8”, “”
$newlist = $newlist -Replace “266”, “”
$newlist = $newlist -Replace “,”, “,”
$newlist = $newlist .TrimEnd(",")
$newlist = $newlist .TrimStart(",")
LogWrite "newlist : $newlist "
}

I want my list to end up like 608,266 and not 60,266.

I also tried using a trim but can’t seem to get it to work.

Do you have any suggestions on how to help a newby out?

 

Any guidance is greatly appreciated!

Is this a single string i.e. “608,8,154,266” or an array of integers? I’m not exactly sure what you are trying to accomplish but it sounds like it would be much easier to work with an array. To do so just use the -split operator which will return an array of strings. Then you can iterate over the array and do whatever you want. Finally if you need to return a single string just use the -join operator to put it back together.

$strnums = "608,8,154,266"
$New = foreach ($num in $strnums -split ",")
{
    if($num -NotIn "8", "154"){$num}
}
$new -join ","

 

It’s important to understand if it’s a string or an array. Mike shows you how to parse the string into an array and then parse. Mike is also converting it back to a string, but it would be best to explain what you are doing with the output:

#No parsing, just numbers
PS C:\Users\rasim> 608,8,154,266 | Where{8,154 -notcontains $_}

608
266

#Parsing string and checking array
PS C:\Users\rasim> "608,8,154,266" -split ',' | Where{8,154 -notcontains $_}

608
266

Just to piggy back on Rob’s comments. In PowerShell a comma is an operator that instantiates an array so:

608,8,154,266

is an array of integers. If the comma is inside enclosed in quotations it is part of the string and not an array operator so:

"608,8,154,266"

Is a single string and not an array. The split operator will take a single string and create an array of strings.

"608,8,154,266" -split ","

This creates an array of strings (even though they are numbers). PowerShell will automatically convert the type if needed as long as there is an appropriate method i.e. toString(), toInt132(). For example you can do this is PowerShell:

"15" -eq 15

and the result will be $true because PS “knows” to convert the string 15 to an integer in order to do the comparison. Be careful with relying on automatic conversions like this because some operators work differently depending on the type. For example:

"15" + 10

You might think this would return 25 but the plus operator will concatenate strings, so PS converts the 10 to a string and performs concatenation instead of converting the “15” to an integer and performing addition.

 

I’m glad I had made the right decision when ordered my first essay here on https://topessaybrands.com service. Not only do these guys take the load off your shoulders, but they also offer their work as a sample to learn from! Now I can pay a small amount of money and got my essay done in few days. The writer is very professional and put all her efforts into my literature subject.