powershell how i can replace any symbol?

Serg On 1 Reputation point
2022-09-06T10:01:01.367+00:00

HI, please help my
how i can replace any symbol?
example:
$ip1 = "10.143.82.29 fe80::3bc6:1a0b:30f0:ffc8 fe80::9c9d:b4ff:fecb:764a 10.130.12.1 fe80::904d:5dff:fe3c:b21e"
$ip1.Replace('fe80::****:****:****:****','')

I would like to get 10.143.82.29 10.130.12.1

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. kilian goëtz 146 Reputation points
    2022-09-06T12:44:09.453+00:00

    Hello,

    Just use a split on space and store it into array like this :

       Powershell  
         
       $ip1 = "10.143.82.29 fe80::3bc6:1a0b:30f0:ffc8 fe80::9c9d:b4ff:fecb:764a 10.130.12.1 fe80::904d:5dff:fe3c:b21e"  
       $CharArray =$ip1.Split()  
       $CharArray[0]  
       $CharArray[3]  
    

    Kind regards, Kilian GOËTZ


  2. Rich Matheisen 45,906 Reputation points
    2022-09-06T14:51:57.54+00:00

    Is the question really "how do remove IPv6 addresses?" Or is it "how to extract IPv4 addresses?"

    To extract the IPv4 addresses:

    $ip1 = "10.143.82.29 fe80::3bc6:1a0b:30f0:ffc8 fe80::9c9d:b4ff:fecb:764a 10.130.12.1 fe80::904d:5dff:fe3c:b21e"  
      
    $IPv4RegEx = '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b'  
    [RegEx]::Matches($ip1, $IPv4RegEx).Value -join " "  
    

    To remove the IPv6 addresses:

    $ip1 = "10.143.82.29 fe80::3bc6:1a0b:30f0:ffc8 fe80::9c9d:b4ff:fecb:764a 10.130.12.1 fe80::904d:5dff:fe3c:b21e"  
      
    $IPv6Regex = '((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*'  
    [RegEx]::Replace($ip1, $IPv6Regex, '')  
    
    0 comments No comments

  3. Limitless Technology 39,511 Reputation points
    2022-09-07T07:25:59.92+00:00

    Hello there,

    With Replace method or -replace operator in PowerShell, we can find and replace any character or (part of) strings with other data.

    Since the PowerShell replace method returns a string, to replace another instance, you can append another replace() method call to the end. PowerShell then invokes the replace() method on the output of the original.

    Use PowerShell to Replace Text in Strings https://devblogs.microsoft.com/scripting/use-powershell-to-replace-text-in-strings/

    I hope this information helps. If you have any questions please let me know and I will be glad to help you out.

    ------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments