是否有Powershell“字符串不包含”cmdlet或语法?

时间:2021-06-05 00:35:22

In Powershell I'm reading in a text file. I'm then doing a Foreach-Object over the text file and am only interested in the lines that do NOT contain strings that are in $arrayOfStringsNotInterestedIn

在Powershell中,我正在阅读一个文本文件。我正在对文本文件做一个Foreach-Object,我只对那些不包含$ arrayOfStringsNotInterestedIn中的字符串的行感兴趣

Does anybody know the syntax for this?

有人知道这个的语法吗?

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

3 个解决方案

#1


39  

If $arrayofStringsNotInterestedIn is an [array] you should use -notcontains:

如果$ arrayofStringsNotInterestedIn是[array],你应该使用-notcontains:

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

or better (IMO)

或更好(IMO)

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

#2


10  

You can use the -notmatch operator to get the lines that don't have the characters you are interested in.

您可以使用-notmatch运算符来获取没有您感兴趣的字符的行。

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

#3


1  

To exclude the lines that contain any of the strings in $arrayOfStringsNotInterestedIn, you should use:

要排除包含$ arrayOfStringsNotInterestedIn中任何字符串的行,您应该使用:

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

The code proposed by Chris only works if $arrayofStringsNotInterestedIn contains the full lines you want to exclude.

如果$ arrayofStringsNotInterestedIn包含要排除的整行,则Chris提出的代码才有效。

#1


39  

If $arrayofStringsNotInterestedIn is an [array] you should use -notcontains:

如果$ arrayofStringsNotInterestedIn是[array],你应该使用-notcontains:

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

or better (IMO)

或更好(IMO)

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

#2


10  

You can use the -notmatch operator to get the lines that don't have the characters you are interested in.

您可以使用-notmatch运算符来获取没有您感兴趣的字符的行。

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

#3


1  

To exclude the lines that contain any of the strings in $arrayOfStringsNotInterestedIn, you should use:

要排除包含$ arrayOfStringsNotInterestedIn中任何字符串的行,您应该使用:

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

The code proposed by Chris only works if $arrayofStringsNotInterestedIn contains the full lines you want to exclude.

如果$ arrayofStringsNotInterestedIn包含要排除的整行,则Chris提出的代码才有效。