2016-11-28 6 views
1

私はいくつかの大きなログ/ダンプをSOAP(ラップなしの1行)で持っています。最初の場合 私はそのようないくつかの単純な選択文字列を行わ: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:\ログ

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(例の一部ではないものをすべてをドロップすることができます\ 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

関連する問題