Grep - PowerShell 风格

Grep - PowerShell Style

我很难让 PowerShell 在 "grep" 之后拆分字符串。我真的很想从每小时网络日志中获取 10 个最大值。

"Grep" 有效,但我无法拆分它:

PS D:\Work\ps> D:\Work\ps\test\grep.ps1 Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'split'. At D:\Work\ps\test\grep.ps1:8 char:2 + $string.split($separator,0, $option) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

https://communary.net/2014/11/10/grep-the-powershell-way/ https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/17/using-the-split-method-in-powershell/

 ##"[com.vendorStuff.utils.Stopwatch]"

 $string = select-string "execution \:" "D:\logs\server.log" -ca
 $separator = "execution \:"
 $option = [System.StringSplitOptions]::RemoveEmptyEntries
 $string.split($separator,1, $option)
 #$data.Split("execution \:")[1]

重要提示:我已经越狱了:'execution :' 我尝试过的几件事:

[string]$Data = sls 'execution \:' "D:\Work\ps\test\server.log" -ca
$data.split('execution \:')[1]

以及:

$Data = (sls 'execution \:' "D:\Work\ps\test\server.log" -ca).tostring().split('execution \:')[1]

谢谢!

您可以在字符串上使用 -split 参数。

$string = "oneToken execution :andAnotherToken"
$string -split "execution :" 
oneToken 
andAnotherToken