2015-02-09 41 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 '/>'