splitting inline to get website address

This topic contains 8 replies, has 5 voices, and was last updated by Profile photo of Dan Potter Dan Potter 1 month, 1 week ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #38049
    Profile photo of Martin Thomsen
    Martin Thomsen
    Participant

    hi

    I need some help/ideas/thoughts...

    I have a file with website adresses (tabs from firefox)
    what I want/need is to be able to split each line to extract the website address..

    example lines are:

    window #1/tab #17 https://www.systemcenterdudes.com/sccm-1511-migration/
    window #1/tab #18 https://www.systemcenterdudes.com/sccm-2012-sql-install-guide/ Complete SCCM 2012 SQL Install guide

    as you can see on line 2 it doesn't just end after the address but has the window title...

    Anyone have any ideas?

    #38051
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant

    without more info, I'm only guessing, but this may be a starting point

    get-content c:\temp\sites.txt | ? {$_ -match '\d https?:'} | % {
    	$_.split(' ')[-1]
    }
    
    #38094
    Profile photo of Martin Thomsen
    Martin Thomsen
    Participant

    hi

    just tried your oneliner... no result at all...

    The code I ran is:


    get-content C:\Users\mrt\AppData\Roaming\Mozilla\Firefox\Profiles\zzatbluv.default\opentabs-ARD-LT-MRT-20160420-0939.txt | ? {$_ -match '\d https?:'} | % { $_.split(' ')[-1]}

    I've copy/pasted a complete file examples:

    http://pastebin.com/Wr04dYPj

    #38102
    Profile photo of Bob McCoy
    Bob McCoy
    Participant
    $pattern = [regex]"\s+(?'url'https*://[\w\./\-\%\?=\+&]+)\s*"
    Get-Content -Path .\test.txt | foreach {
        if ($line -match $pattern)
        {
            $Matches['url']
        }
    }

    This seems to be a pretty good match for your sample text file.

    #38205
    Profile photo of Martin Thomsen
    Martin Thomsen
    Participant

    hi Bob

    thx... looks great...
    I tried this:


    $Path="C:\Users\mrt\AppData\Roaming\Mozilla\Firefox\Profiles\zzatbluv.default\"
    $file="opentabs-ARD-LT-MRT-20160421-1502.txt"
    $pattern = [regex]"\s+(?'url'https*://[\w\./\-\%\?=\+&]+)\s*"
    Get-Content -Path "$Path$File" | foreach {
    if ($line -match $pattern)
    {
    $Matches['url']
    write-host $matches

    }
    }

    but no results...

    the get-content produces results..

    i tried adding else {write-host $file}

    but still nothing...

    #38209
    Profile photo of Chris Bakker
    Chris Bakker
    Participant

    This works:
    $pattern = [regex]"\s+(?'url'https*://[\w\./\-\%\?=\+&]+)\s*"
    Get-Content -Path d:\temp\test\test.txt | foreach-object {
    if ($_ -match $pattern)
    {
    $Matches['url']
    }
    }

    #38215
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    You don't need the write-host. And you would only use $matches['url']. And since you didn't tell us what you want to do with the data (write to screen, file, etc.) it is essentially demo code showing how to extract the data.

    If you tell us more we can probably give you a more complete solution.

    #50568
    Profile photo of Martin Thomsen
    Martin Thomsen
    Participant

    hi all..

    thank you for the suggestions... It kindda stranded for a while but after a crash today I decided that now was the time..

    I fiddled a lot with how to open all results in tabs but nothing worked ... I ended up crashing my computer because it opened so many instances of firefox that the system halted...

    finally I went the KISS way and did this:

    & 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
    start-sleep 5
    $pattern = [regex ]"\s+(?'url'https*://[\w\./\-\%\?=\+&]+)\s*"
    Get-Content -Path C:\Users\mrt\AppData\Roaming\Mozilla\Firefox\Profiles\rluonxjq.default\opentabs-ARD-LLT-MRT-20160815-1513.txt | foreach-object {
    if ($_ -match $pattern)
    {
    Start-Process $matches ['url']
    }
    }

    #50587
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Powershell almost always has a real simple way to manipulate strings without ridiculous regex patterns.

    As you said, KISS.

    $text.split(' ') -match ':'

    • This reply was modified 1 month, 1 week ago by Profile photo of Dan Potter Dan Potter.
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.