Existe-t-il une chaîne PowerShell & # 8220; ne contient pas & # 8221; cmdlet ou syntaxe?

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

  •  09-06-2019
  •  | 
  •  

Question

Dans PowerShell, je lis un fichier texte. Je suis ensuite en train de faire un objet Foreach sur le fichier texte et je ne m'intéresse qu'aux lignes qui ne contiennent PAS de chaînes qui se trouvent dans $ arrayOfStringsNotInterestedIn .

Quelle est la syntaxe pour cela?

   Get-Content $filename | Foreach-Object {

Dans PowerShell, je lis un fichier texte. Je suis ensuite en train de faire un objet Foreach sur le fichier texte et je ne m'intéresse qu'aux lignes qui ne contiennent PAS de chaînes qui se trouvent dans $ arrayOfStringsNotInterestedIn .

Quelle est la syntaxe pour cela?

<*>}
Était-ce utile?

La solution

Si $ arrayofStringsNotInterestedIn est un [tableau], vous devez utiliser -notcontains:

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

Si $ arrayofStringsNotInterestedIn est un [tableau], vous devez utiliser -notcontains:

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

Si $ arrayofStringsNotInterestedIn est un [tableau], vous devez utiliser -notcontains:

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

Si $ arrayofStringsNotInterestedIn est un [tableau], vous devez utiliser -notcontains:

<*>

ou mieux (IMO)

<*>) { $) }

ou mieux (IMO)

<*>}

ou mieux (IMO)

<*>) { $) }

ou mieux (IMO)

<*>

Autres conseils

Vous pouvez utiliser l'opérateur -notmatch pour obtenir les lignes ne contenant pas les caractères qui vous intéressent.

     Get-Content $FileName | foreach-object { 
     if (

Vous pouvez utiliser l'opérateur -notmatch pour obtenir les lignes ne contenant pas les caractères qui vous intéressent.

<*> -notmatch $arrayofStringsNotInterestedIn) { $) }

Pour exclure les lignes contenant l'une des chaînes de $ arrayOfStringsNotInterestedIn, vous devez utiliser:

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

Le code proposé par Chris ne fonctionne que si $ arrayofStringsNotInterestedIn contient les lignes complètes que vous souhaitez exclure.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top