Last active
May 9, 2017 12:48
-
-
Save Dan1el42/e8b38f583f00972f80c9c80d4e82ac0d to your computer and use it in GitHub Desktop.
Example 3 and 4 for https://powershell.org/forums/topic/sort-csv-data/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csvFileName = 'C:\My\Input.csv' | |
# Example 3 - Using the -match operator with a more complex regular expression to extract the IP address | |
Import-Csv -Path $csvFileName | | |
ForEach-Object { | |
if ($PSItem.URL -match '(?<IPAddress>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})') { | |
$Matches.IPAddress | |
} | |
} | |
# Example 4 - Using the -match operator with a more regular expression to extract the IP address | |
# and casting to IPAddress to ensure we get a valid IP address | |
Import-Csv -Path $csvFileName | | |
ForEach-Object { | |
if ($PSItem.URL -match '(?<IPAddress>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})') { | |
([IPAddress] $Matches.IPAddress).IPAddressToString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment