2

我想添加一个输入变量,允许我添加任意数量的条件。

例如:添加变量-and ($_ -notmatch '67'

$file = "\Input\27532.csv"
$outFile = "\Output\27532.csv"

$content= Get-Content $file | Where-Object { ($_ -notmatch '24') -and ($_ -notmatch '67')  } | Set-Content $outfile
4

1 回答 1

4

使用带有正则表达式交替( ) 的单个 -notmatch操作,它允许您传递无限数量的子字符串: |

$valuesToExclude = '24', '67', '42'

$content= Get-Content $file | 
  Where-Object { $_ -notmatch ($valuesToExclude -join '|') } | 
   Set-Content $outfile

注意:以上假设$valuesToExclude只包含不包含正则表达式元字符的值(例如,.);如果有机会,并且您希望这些字符被逐字解释,请调用[regex]::Escape()以下值:
($valuesToExclude.ForEach({ [regex]::Escape($_) }) -join '|')

于 2021-06-30T19:22:05.297 回答