Cutting part of url in powershell

I have multiple urls that would have to cut and separate the first part of each url i.e. example1.com

, example2.com

, example3.com

Of each line and stored in a variable

Content in url.csv

https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4
https://example2.com/v1/test/14nf-d7jc-54lf-fd90-fds8
https://example3.com/v1/test/bd38-17gd-2h65-0j3b-4jf6

Script:

$oldurl = Import-CSV "url.csv"
$newurl = $oldurl.list -replace "https://" 

      

This will override https://

, however the rest of them cannot be hardcoded as these values ​​can change.

What can be changed to change the code needed to shorten anything from and after /v1/

along with https://

?

+3


source to share


2 answers


$list = @(
    "https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4",
    "https://example2.com/v1/test/14nf-d7jc-54lf-fd90-fds8",
    "https://example3.com/v1/test/bd38-17gd-2h65-0j3b-4jf6"
)

$result = $list | %{
    $uri = [System.Uri] $_

    $uri.Authority
}

$result

      



Look at the System.Uri properties to potentially gather the information you need in the list of results.

+3


source


This will cut anything after "/ v1 /" and itself. Is this what you want?



 $string = "https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4"
 $string = $string -replace "https://" 
 $pos = $string.IndexOf("/v1/")
 $result = $string.Substring(0, $pos)
 $result

 Output: example1.com

      

0


source







All Articles