web-dev-qa-db-ja.com

PowerShellの「文字列に含まれない」コマンドレットまたは構文はありますか?

PowerShellでは、テキストファイルを読んでいます。次に、テキストファイルに対してForeach-Objectを実行し、$arrayOfStringsNotInterestedInにある文字列を含まない行にのみ関心があります。

これの構文は何ですか?

   Get-Content $filename | Foreach-Object {$_}
27
Guy

$ arrayofStringsNotInterestedInが[配列]の場合、-notcontainsを使用する必要があります。

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

以上(IMO)

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

-notmatch演算子を使用して、関心のある文字がない行を取得できます。

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

$ arrayOfStringsNotInterestedInの文字列のいずれかを含む行を除外するには、次を使用する必要があります。

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

Chrisが提案するコードは、$ arrayofStringsNotInterestedInに除外する完全な行が含まれている場合にのみ機能します。

1
Bruno Gomes