提取String Powershell

时间:2016-09-09 09:06:46

标签: powershell powershell-ise

我试图从powershell中的以下文本中提取loankey:

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

2 个答案:

答案 0 :(得分:1)

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

$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 }

答案 1 :(得分:0)

select-string -pattern "LOANKEY:(.*)\)" .\errorlog.txt | % {"$($_.matches.groups[1])"}
然后你会得到你想要的东西。 请参考此处:How to get the captured groups from Select-String?