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



我试图比较两个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}}

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

使用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 }

最新更新