提取字符串 Powershell

Extracting String Powershell

我正在尝试从 powershell 中的以下文本中提取贷款密钥:

Test\errorlog.txt:4::293:StepName:WARNING StepTime:9/9/2016 9:01:06 AM StepMessage:ERROR(KEY1:7798850,LOANKEY:11111)::POST PAYMENT StepNotes:[[WARNING]]
Test\errorlog.txt:8::299:StepName:WARNING StepTime:9/9/2016 9:01:11 AM StepMessage:ERROR(KEY1:7798870,LOANKEY:54321)::POST PAYMENT StepNotes:[[WARNING]]
Test\errorlog.txt:12::305:StepName:WARNING StepTime:9/9/2016 9:01:14 AM StepMessage:ERROR(KEY1:7798863,LOANKEY:12345)::POST PAYMENT StepNotes:[[WARNING]]

我目前有以下内容来过滤错误日志,直到你在上面看到它:

cls

$path = 'C:\Test\errorlog.txt'

$script = select-string -pattern "LOANKEY:" $path

提到你可以使用正则表达式的输入:

$txt = "Test\errorlog.txt:4::293:StepName:WARNING StepTime:9/9/2016 9:01:06 AM StepMessage:ERROR(KEY1:7798850,LOANKEY:11111)::POST PAYMENT StepNotes:[[WARNING]]",
"Test\errorlog.txt:8::299:StepName:WARNING StepTime:9/9/2016 9:01:11 AM StepMessage:ERROR(KEY1:7798870,LOANKEY:54321)::POST PAYMENT StepNotes:[[WARNING]]",
"Test\errorlog.txt:12::305:StepName:WARNING StepTime:9/9/2016 9:01:14 AM StepMessage:ERROR(KEY1:7798863,LOANKEY:12345)::POST PAYMENT StepNotes:[[WARNING]]"

$txt | % { [Regex]::Match($_, "(?<=LOANKEY:)(\d+)").Value }
select-string -pattern "LOANKEY:(.*)\)" .\errorlog.txt | % {"$($_.matches.groups[1])"}

那么你会得到你想要的。 此处参考: