Esiste un cmdlet o una sintassi di PowerShell "la stringa non contiene"?

StackOverflow https://stackoverflow.com/questions/74957

  •  09-06-2019
  •  | 
  •  

Domanda

In PowerShell sto leggendo un file di testo.Sto quindi eseguendo un Foreach-Object sul file di testo e sono interessato solo alle righe che NON contengono stringhe presenti in $arrayOfStringsNotInterestedIn.

Qual è la sintassi per questo?

   Get-Content $filename | Foreach-Object {$_}
È stato utile?

Soluzione

Se $arrayofStringsNotInterestedIn è un [array] dovresti usare -notcontains:

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

o meglio (IMO)

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

Altri suggerimenti

Puoi utilizzare l'operatore -notmatch per ottenere le righe che non contengono i caratteri che ti interessano.

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

Per escludere le righe che contengono una qualsiasi delle stringhe in $arrayOfStringsNotInterestedIn, dovresti usare:

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

Il codice proposto da Chris funziona solo se $arrayofStringsNotInterestedIn contiene le righe complete che desideri escludere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top