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

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

有人知道它的语法吗?

   Get-Content $filename | Foreach-Object {$_}
请先 登录 后评论
本文连接: http://www.china-sunrider.com.cn/question/8603
source: https://stackoverflow.com/questions

3 个回答

Mark Schill

可以使用-notmatch操作符获取不包含感兴趣字符的行。

     Get-Content $FileName | foreach-object { 
     if ($_ -notmatch $arrayofStringsNotInterestedIn) { $) }
请先 登录 后评论
Chris Bilson

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

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

或者更好(IMO)

Get-Content $FileName | where { $arrayofStringsNotInterestedIn -notcontains $_}
请先 登录 后评论
Bruno Gomes

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

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

Chris建议的代码只在$arrayofStringsNotInterestedIn包含您想要排除的全部行时才有效。

请先 登录 后评论
user contributions licensed under CC BY-SA.