Home > Mobile >  Powershell - Remove all text after second Colon
Powershell - Remove all text after second Colon

Time:12-07

I have been attempting this all day with various code snippets from stack overflow but just cant find the special sauce!

I have a text file with hundreds of URL's in the following format

http://42.231.225.226:36958/Mmmm.m

I just want to keep http://42.231.225.226 in the text file and the rest of the URL can go.

I have been working on deleting everything after the second : (including the second :) but i cant get the right RegEx pattern.

if someone knows of the correct regex command or even the correct split command to achieve my desired outcome I would be very grateful.

Thankyou!

various snippets from stackoverflow - turns out im a bit hopeless

CodePudding user response:

No need to use regex. Let's assume that you have txt file with links - url.txt

http://42.231.225.223:36958/Mmmm.m
http://42.231.225.224:36958/Mmmm.asdf
http://42.231.225.225:369538/xxx.b

then using this snippet

$data = get-content url.txt
$data | ForEach-Object {
  $items = $_.split(":")
  write-host  (-join('http:',$items[1]))
}

will produce this output

http://42.231.225.223
http://42.231.225.224
http://42.231.225.225

CodePudding user response:

You can use the type accelerator for [url] to do some of the work for you:

 [uri]$u="http://42.231.225.223:36958/Mmmm.m"

if you look at the resulting object:

$u
AbsolutePath   : /Mmmm.m
AbsoluteUri    : http://42.231.225.223:36958/Mmmm.m
LocalPath      : /Mmmm.m
Authority      : 42.231.225.223:36958
HostNameType   : IPv4
IsDefaultPort  : False
IsFile         : False
IsLoopback     : False
PathAndQuery   : /Mmmm.m
Segments       : {/, Mmmm.m}
IsUnc          : False
Host           : 42.231.225.223
Port           : 36958
Query          :
Fragment       :
Scheme         : http
OriginalString : http://42.231.225.223:36958/Mmmm.m
DnsSafeHost    : 42.231.225.223
IdnHost        : 42.231.225.223
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

You can see that you'll get the host and the segments of the url. What Scheme was used etc.

So to get to the point you are asking for you can just do this:

 "$($u.scheme)://$($u.host)"
 http://42.231.225.223

Now just put that in a loop and your home free.

  • Related