2008-09-16 55 views
23

在Powershell中,我正在阅读文本文件。然后我在文本文件上做了一个Foreach对象,并且只对不包含$ arrayOfStringsNotInterestedIn中的字符串的行感兴趣。是否有Powershell“字符串不包含”cmdlet或语法?

有没有人知道这个语法?

Get-Content $filename | Foreach-Object {$_} 
+0

您可能会将* -notmatch *或* -notlike *与数组中的每个字符串结合使用。 – 2008-09-16 17:50:42

回答

35

如果$ arrayofStringsNotInterestedIn是一个[数组]你应该使用-notcontains :

Get-Content $FileName | foreach-object { ` 
    if ($arrayofStringsNotInterestedIn -notcontains $_) { $) } 

或更好(IMO)

Get-Content $FileName | where { $arrayofStringsNotInterestedIn -notcontains $_} 
10

可以使用-notmatch接线员给那些没有你感兴趣的字符线。

 Get-Content $FileName | foreach-object { 
    if ($_ -notmatch $arrayofStringsNotInterestedIn) { $) } 
+0

http://technet.microsoft.com/en-us/magazine/cc137764.aspx – 2008-09-16 17:54:50

1

排除包含任何在$ arrayOfStringsNotInterestedIn琴弦的线,你应该使用:

(Get-Content $FileName) -notmatch [String]::Join('|',$arrayofStringsNotInterestedIn) 

克里斯提出的代码只有$ arrayofStringsNotInterestedIn包含要排除全行工作。

相关问题