嵌套循环以比较Powershell中的两个CSV并删除重复项

时间:2016-07-20 00:21:00

标签: csv powershell nested

我正在尝试比较两个CSV,如果行名称与另一个匹配,则从一个中删除整个对象。现在我将它设置为等于$test变量进行验证。第一线工作;为了清楚起见,我把它包括在内。

#Compare and Output Perfect Matches
    $perfect=compare-object (import-csv $bpa) (import-csv $peak) -Property "Op_Area","Line_Name","From_St","From_Nd","To_St","To_Nd" -IncludeEqual -ExcludeDifferent 
#Remove Perfect Matches from Original Two Lists
    $test=import-csv $peak|%{if($_.line_name -eq $perfect|%{$_.line_name}){Remove-Item}}

我的最终目标是找到两个列表之间的匹配(在第一行中完成),从两者中删除,然后切换几行并找到第二组匹配。

1 个答案:

答案 0 :(得分:1)

使用Select-Object -ExpandProperty Line_Name只从匹配的条目中获取Line_Name值:

# Store the $peak csv in a variable, we'll need it later
$peakData = Import-Csv $peak
$perfect = Compare-Object (Import-Csv $bpa) $peakData -Property "Op_Area","Line_Name","From_St","From_Nd","To_St","To_Nd" -IncludeEqual -ExcludeDifferent |Select-Object -ExpandProperty Line_Name

然后使用Where-Object过滤掉这些行:

$test = $peakData |Where-Object { $perfect -notcontains $_.Line_Name }