在 PowerShell 中,我正在读取文本文件。然后,我在文本文件上执行 Foreach-Object,并且只对不包含以下字符串的行感兴趣 $arrayOfStringsNotInterestedIn.

这个的语法是什么?

   Get-Content $filename | Foreach-Object {$_}
有帮助吗?

解决方案

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

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

或更好(国际海事组织)

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

其他提示

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

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

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

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

Chris 提出的代码仅在 $arrayofStringsNotInterestedIn 包含您要排除的完整行时才有效。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top