¿Existe un cmdlet o sintaxis de Powershell "string does not contain"?


En Powershell estoy leyendo en un archivo de texto. Luego estoy haciendo un Foreach-Object sobre el archivo de texto y solo estoy interesado en las líneas que NO contienen cadenas que están en arra arrayOfStringsNotInterestedIn

¿Alguien conoce la sintaxis para esto?

   Get-Content $filename | Foreach-Object {$_}
 23
Author: Guy, 2008-09-16

3 answers

Si arra arrayofStringsNotInterestedIn es un [array] deberías usar-notcontains:

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

O mejor (OMI)

Get-Content $FileName | where { $arrayofStringsNotInterestedIn -notcontains $_}
 39
Author: Chris Bilson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-16 17:56:12

Puede usar el operador-notmatch para obtener las líneas que no tienen los caracteres que le interesan.

     Get-Content $FileName | foreach-object { 
     if ($_ -notmatch $arrayofStringsNotInterestedIn) { $) }
 10
Author: Mark Schill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-29 13:54:53

Para excluir las líneas que contienen cualquiera de las cadenas en arra arrayOfStringsNotInterestedIn, debe usar:

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

El código propuesto por Chris solo funciona si $arrayofStringsNotInterestedIn contiene las líneas completas que desea excluir.

 1
Author: Bruno Gomes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-27 14:43:45