2014-12-19 68 views
3

我想通过文件循环并删除不需要的行。文件中的行具有不会更改的唯一编号。到目前为止,我没有在foreach循环中删除该行,但分开执行。通过文件循环并删除几行不同的文本

$FSEFiles=get-childitem E:\FSE*.txt 

foreach ($file in $FSEFiles) 

{ 

    try 

    { 
     (Get-Content $file.PSPath) | 
     Foreach-Object { Where-Object { $_ -notmatch "[email protected]" } } | 
     Foreach-Object { Where-Object { $_ -notmatch "[email protected]" } } | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     #Foreach-Object {Where-Object { $_ -notmatch "[email protected]"}} | 
     Set-Content $file.PSPath 

     write-host $file updated 
    } 
    catch 
    { 
     write-host Error editing $file 
    } 
} 

我错过了什么可以让这个循环在每一行工作吗?

回答

3

既然你已经在使用正则表达式让所有这些要求合并成一个漂亮的正则表达式字符串

$regex = "([email protected]|[email protected]|1752[6-8]@|1753[0-2]@)" 

$data = Get-Content $file.PSPath 
$data | Where-Object{$_ -notmatch $regex} | Set-Content $file.PSPath 

也许类似的东西会流线的。没有真正的测试,但应该工作。你有什么可能有效的工作,但有些东西似乎是foreachwhere(因此是问题)的组合,但尝试和解决,我会认为是多余的。

+0

完美!这正是我所需要的。 谢谢。 – Alan

+0

很高兴帮助。请考虑在左边的选票下标上这个答案。 – Matt

相关问题