2

Im trying to extract the loankey from the following text in 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]]

I have currently got the following to filter down the error log down to how you see it above:

cls

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

$script = select-string -pattern "LOANKEY:" $path
Aatif Akhter
  • 2,126
  • 1
  • 25
  • 46
NickBuckley
  • 77
  • 1
  • 1
  • 9

2 Answers2

1

Having mentioned input you can use regex:

$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 }
Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27
1
select-string -pattern "LOANKEY:(.*)\)" .\errorlog.txt | % {"$($_.matches.groups[1])"}

then you'll get what you want. reference here: How to get the captured groups from Select-String?

Community
  • 1
  • 1
proQuest
  • 26
  • 1
  • the Answer from @paweł-dyl is also great, just with "more" we can save the variable definition: `more .\errorlog.txt | % { [Regex]::Match($_, "(?<=LOANKEY:)(\d+)").Value }` – proQuest Sep 09 '16 at 09:38