Powershell 使用-match 从文本文件中获取特定单词

Powershell get specific word from textfile using -match

我这里有一行代码来搜索特定的单词并输出这个单词所在的行。

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

我可以输出包含特定单个 'ms' 单词的特定行吗?因为我得到的输出是:

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

因为ID里面还有'ms'关键词

我只想要一个输出:

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

-匹配是否可行?谢谢!

尝试以下操作:

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

编辑:

根据RegExr,以下表达式符合您的要求:

[0-9]* ms

因此:

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

应该完全匹配。