2016-11-28 80 views
1

我有一些大的日誌/轉儲與SOAP(1line沒有包裝)。 對於第一次我做了一些簡單的選擇串那樣:如何在Powershell中分割不規則字符串?

$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 

結果是這樣的:

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> 

我怎麼能放下一切究竟是不是我的SOAP(例如部分:d:\日誌\ Test \ test1.txt:4000:g .vL.Yb ...'P .......)

回答

3

D:\log\Test\test1.txt:1:是由Select-String(在文件中找到的匹配的完整路徑和行號)添加的信息。

如果你有單行XML字符串文本文件,只是想從東西線這樣開始刪除一些多餘的內容可能會做:

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

此枚舉在給定的所有.txt文件讀取其內容,刪除行首(^)和XML前奏(<\?xml)之間的字符串,然後將修改後的文本寫回文件。

+0

謝謝我無法得到這個正則表達式^。*?(<\?xml) - 這改變了一切。 – tadamsky

-1

不知道一大堆關於SOAP的知識,但有一點信息可以可能有助於解析字符串。做到這一點最簡單的方法很可能是遍歷你的字符串數組,只是拉子從行的開頭去的<所以像

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

也可以用做索引位置正則表達式,如果你願意,但這是我腦海中更多的工作。

+0

如果在<?之前取<<「,它會返回錯誤的結果??XML – tadamsky