英文:
Filter a CSV file with multiple conditions using PowerShell
问题
Sure, here's the translated code portion:
$path = "C:\users\knot22\PowerShell"
Import-CSV "$($path)\DataSource.csv" | Where-Object { $_.Branch -NotLike '*102*' -and $_.Name -NotLike '*Admin*' } |
Export-CSV "$($path)\DataTarget.csv" -NoTypeInformation -UseQuotes Never
This modified PowerShell script uses the Where-Object
cmdlet to combine the conditions with -and
so that it runs successfully and keeps the syntax concise. This code is compatible with PowerShell 7.
英文:
Here is a small sample of data in a CSV file that needs to be filtered to remove all lines that contain Branch 102 or Name equals Admin.
DataSource.csv
branch,ID,name
102,11056,Jones
103,11057,Henry
102,22000,Admin
103,22001,Admin
102,22002,White
103,22003,George
Here is the first version my PowerShell script, demo.ps1, that works -
$path = "C:\users\knot22\PowerShell"
Import-CSV "$($path)\DataSource.csv" | Where Branch -NotLike '*102*' | Where Name -NotLike '*Admin*' |
Export-CSV "$($path)\DataTarget.csv" -NoTypeInformation -UseQuotes Never
The resulting output of DataTarget.csv is -
branch,ID,name
103,11057,Henry
103,22003,George
However, I'd like to modify demo.ps1 so that the conditions are in a single Where
block. I've tried this
$path = "C:\users\knot22\PowerShell"
Import-CSV "$($path)\DataSource.csv" | Where Branch -NotLike '*102*' -or Name -NotLike '*Admin*' |
Export-CSV "$($path)\DataTarget.csv" -NoTypeInformation -UseQuotes Never
but it results in an error
> "Cannot bind parameter because parameter 'NotLike' is specified more
> than once. To provide multiple values to parameters that can accept
> multiple values, use the array syntax. For example, "-parameter
> value1,value2,value3".
This error message implies that the multiple conditions are intended for one parameter. In the use case above, the multiple conditions apply to different parameters.
Is there a way to modify the syntax in the second version of demo.ps1 that attempts to combine the conditions with -and
so that it runs successfully? I'm looking to keep the syntax concise.
Also, I'm using PowerShell 7.
答案1
得分: 5
Where-Object
cmdlet(别名 Where
)有两种不同的过滤模式 - 一种用于简单的属性比较(也称为简化语法,这是您当前正在使用的方式),另一种用于更复杂的过滤情况。
对于后者,您需要提供一个脚本块,然后将其用作对每个输入项的谓词:
Where { $_.Branch -notlike '*102*' -or $_.Name -notlike '*Admin*' }
请注意,这并不等同于您当前的管道 - 对于这种情况,您会想要使用 -and
运算符:
Where { $_.Branch -notlike '*102*' -and $_.Name -notlike '*Admin*' }
英文:
The Where-Object
cmdlet (alias Where
) has two distinct modes of filtering - one for simple property comparison (aka. simplified syntax, which is what you're currently using), and one for more complex filtering scenarios.
For the latter, you'll need to supply a scriptblock that is then used as a predicate against each input item:
Where { $_.Branch -notlike '*102*' -or $_.Name -notlike '*Admin*' }
Beware that this isn't the equivalent of your current pipeline - for that, you'll want to use the -and
operator:
Where { $_.Branch -notlike '*102*' -and $_.Name -notlike '*Admin*' }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论