2015-02-09 61 views
1

這是我的腳本,用於刪除其中包含/>的所有行。我有一個新的要求,即離開所有具有index="1"/>的行。Powershell選擇字符串的用法

我想離開的實線示例爲<LOTLEVEL index="1"/>。邏輯將是,如果行包含index="1"/>請保留行,如果行包含/>但不包含index="1"/>刪除行。

現有的腳本看起來像這樣。任何建議都會很棒。越簡單越好。

$SourceDir = Split-Path $MyInvocation.MyCommand.Path 
Get-Content "$SourceDir\*.txt" | Select-String -pattern '/>' -notmatch -pattern | foreach {$_.Line} | out-file "$SourceDir\clean.xml" -encoding ascii -width 1000 
+0

請編輯您的實際問題,包括修正後,而不是將其嵌入在評論中。讀者在查看評論之前很久就會看到這個問題。 – kdopen 2015-02-09 14:38:59

回答

0

嘗試是這樣的:

Get-Content "$SourceDir\*.txt" | where {$_ -notmatch '"/>'} | Select-String '/>' 

如果你需要的東西多一點具體的嘗試:

Get-Content "$SourceDir\*.txt" | where {$_ -notmatch 'index="\d"(\s+)?/>'} | Select-String '/>'