Loops PowerShell-Csv:具有条件的foreach?
我必须导入一个csv文件,我想在foreach中直接添加一个条件。 我的意思是: 要读取csv,我需要(是,我的分隔符是\): 那么我会:Loops PowerShell-Csv:具有条件的foreach?,loops,powershell,foreach,conditional-statements,Loops,Powershell,Foreach,Conditional Statements,我必须导入一个csv文件,我想在foreach中直接添加一个条件。 我的意思是: 要读取csv,我需要(是,我的分隔符是\): 那么我会: foreach($line in $csv) { #Actions } 我想做什么,但我不知道是否可能: for($i =1; $i -lt $myarray.Length; $i++) { foreach($line in $csv) | Where $line.column2 -eq $myarray[$i] { #Action
foreach($line in $csv)
{
#Actions
}
我想做什么,但我不知道是否可能:
for($i =1; $i -lt $myarray.Length; $i++)
{
foreach($line in $csv) | Where $line.column2 -eq $myarray[$i]
{
#Actions only if $line.column2 is equal to $myarray[$i]
}
}
所以我得到了一个数组,里面有名字。我只想在csv中的一列与数组中的信息匹配的情况下执行#操作。如果没有,我想更改$csv中的$line
我写的想法看起来很沉重,我认为有一种更好、更快(更难、更强)的方法来做到这一点
如果有人以前已经使用过,请告诉我:)
提前感谢,
尼科
我为我的英语道歉;)
编辑:我刚刚意识到我可以做一个if条件:
for($i =1; $i -lt $myarray.Length; $i++)
{
foreach($line in $csv)
{
if($line.column2 -eq $myarray[$i]
{
#Actions
}
}
}
但我仍然认为a:| Where#条件更好…如果在循环内部,可以使用以下一般方法
Import-Csv ... | Foreach-Object{
if(something)
{
#do something with the current object and write it back to the pipeline
$_ = ...
$_
}
else
{
# write the object back to the pipeline
$_
}
}
谢谢:)我用这个。谢谢。是的,**如果($.HeaderName-eq desiredValue){}有效,或者对象也可以在哪里工作。
Import-Csv ... | Foreach-Object{
if(something)
{
#do something with the current object and write it back to the pipeline
$_ = ...
$_
}
else
{
# write the object back to the pipeline
$_
}
}