Search code examples
powershell-4.0

Powershell get specific word from textfile using -match


I have here line of code to search for specific word and output the line which this word is into.

Get-Content -path "C:\Apps\CamstarPerMon_logs\GetEqStatus - $getdate.log" | % {if($_ -match 'ms') {Write-Host $_}}

can i output specific line with specific single 'ms' word in it? because the output i get are:

INF: 2019-09-23 05:50:04.674 | Server1 | ID:12395msrh0000
INF: 2019-09-23 05:50:04.721 | Server1 | 47 ms
INF: 2019-09-23 05:50:05.252 | Server2 | ID:12395msrh0x9c
INF: 2019-09-23 05:50:05.346 | Server2 | 94 ms

because the ID also have 'ms' keyword in it.

I want an output strictly only for this:

INF: 2019-09-23 05:50:04.721 | Server1 | 47 ms
INF: 2019-09-23 05:50:05.346 | Server2 | 94 ms

is this possible for -match? thanks!


Solution

  • Try the following:

    Get-Content -path "C:\Apps\CamstarPerMon_logs\GetEqStatus - $getdate.log" | % {if($_ -match '[0-9]* ms') {Write-Host $_}}
    

    EDIT:

    According to RegExr, the following expression matches your requirement:

    [0-9]* ms

    Thus:

    {if($_ -match '[0-9]* ms') {Write-Host $_}}

    Should match perfectly.