1

I have some big logs/dumps with SOAP (1line without wrapping). For first I done some simple Select-string like that:

$where = "D:\log\Test\"
$what = Get-ChildItem $where -Filter "*.txt"
$regex= "(?=<\?xml).*(Envelope>)"
$Path="d:\Log\"
$Result = "D:\Log\wynik2.log"
$string = select-string -Path $what -Pattern $regex
$string

Result is like:

D:\log\Test\test1.txt:1:g .vI.Y....(A..P.......<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">some text1</soap:Body></soap:Envelope>
D:\log\Test\test1.txt:2:g .vJ.YiB..(...P....R..<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">some text2</soap:Body></soap:Envelope>
    ...
D:\log\Test\test1.txt:4000:g .vL.Yb...'...P.......<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">some text2</soap:Body></soap:Envelope>

How can I drop everything what is not part of my SOAP (example: D:\log\Test\test1.txt:4000:g .vL.Yb...'...P.......)

tadamsky
  • 23
  • 5

2 Answers2

3

D:\log\Test\test1.txt:1: are information added by Select-String (full path and line number of the match found in the file).

If you have text files with single-line XML strings and just want to remove some cruft from the beginning of the lines something like this might do:

Get-ChildItem $where -Filter '*.txt' | ForEach-Object {
  (Get-Content $_.FullName) -replace '^.*?(<\?xml)', '$1' |
    Set-Content $_.FullName
}

This enumerates all .txt files in the given folder, reads their content, removes the string between the beginning of the line (^) and the XML prelude (<\?xml), then writes the modified text back to the file.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
-1

don't know a whole lot about SOAP but with a bit more info can probably help parse the string. Easiest way to do it would probably be to loop through your array of strings and just pull a substring that goes from the beginning of the line to the index position of < so something like

foreach($s in $string){
  $s.substring(0,$s.indexOf('<'))
}

Could also do it with a regex if you prefer but that's a good bit more work in my mind.

Mike Garuccio
  • 2,588
  • 1
  • 11
  • 20