2008-09-16 30 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包含要排除全行工作。

相關問題