Compare Two CSVs, match the columns on 2 or more Columns, export specific columns from both csvs with powershell

后端 未结 5 782
傲寒
傲寒 2021-01-24 06:33

i Have 2 CSV\'s

left.csv

Ref_ID,First_Name,Last_Name,DOB
321364060,User1,Micah,11/01/1969
946497594,User2,Acker,05/28/1960
887327716,User3,Aco,06/26/1950         


        
5条回答
  •  梦毁少年i
    2021-01-24 07:29

    $left = Import-Csv C:\left.csv
    $right = Import-Csv C:\right.csv
    
    Compare-Object -ReferenceObject $left -DifferenceObject $right -Property First_Name,Last_Name,DOB -IncludeEqual -ExcludeDifferent | 
        ForEach-Object {
            $iItem = $_
            $ileft = $left.Where({$_.First_Name -eq $iItem.First_Name -and $_.Last_Name -eq $iItem.Last_Name -and$_.DOB -eq $iItem.DOB})
            $iright = $right.Where({$_.First_Name -eq $iItem.First_Name -and $_.Last_Name -eq $iItem.Last_Name -and$_.DOB -eq $iItem.DOB})
            [pscustomobject]@{
                Ref_ID=$ileft.Ref_ID
                first_name=$ileft.first_name
                last_name=$ileft.last_name
                DOB=$ileft.DOB
                Document_Type=$iright.Document_Type
                Filename=$iright.Filename
            }
        } | Export-Csv C:\Combined.csv -NoTypeInformation
    

提交回复
热议问题