Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to display matching object via regex based on pattern

$
0
0

Hi there. Earlier i had a recent post, where many of you kind folks helped me out with a regex question i had. I have a similar search which i'm 95% successful but needed a little assistance. I'm trying to display an object in a line based on pattern. The field i need to display on the console (write-host) is highlighted in bold (it can be alphanumeric/alpha/numeric/anything). My code is below and works but it outputs the full line, i just need the one object to be displayed (the bolded part). I went ahead and used Bob McCoy's code and it works very well, but i'm lost on the last part. Thanks

http://powershell.com/cs/forums/t/23404.aspx

A sample full line i'm searching for :-

<row><td>IMAGE_VERSION_1.2.3.4</td><td>SOFTWARE_PACK</td><td>&lt;ISProjectDataFolder&gt;</td><td>ServiceName<

 

My code below:-

$pattern = [regex]"<row><td>(.*)</td><td>SOFTWARE_PACK</td><td>&lt;ISProjectDataFolder&gt;</td><td>ServiceName<"

# parsing thru file searching for matching pattern

$lineNum = 0

$file = "C:\file.txt"

$content = Get-Content -Path $file

foreach ($line in $content) 

{

    if ($line -match $pattern)

    {

        $content[$lineNum] = $line | Write-Host 

    }

    $lineNum++

}


Viewing all articles
Browse latest Browse all 6937

Trending Articles