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

后端 未结 5 792
傲寒
傲寒 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条回答
  •  长情又很酷
    2021-01-24 07:22

    You could create you own key from each csv, then add from each csv to a new hashtable using this key.

    Step through this in a debugger (ISE or VSCode) and tailor it to what you need... Add appropriate error checking as you need depending on the sanity of your data. Some statements below are just for debugging so you can inspect what's happening as it runs.

    # Ref_ID,First_Name,Last_Name,DOB
    $csv1 = @'
    321364060,User1,Micah,11/01/1969
    946497594,User2,Acker,05/28/1960
    887327716,User3,Aco,06/26/1950
    588496260,User4,John,05/23/1960
    565465465,User5,Jack,07/08/2020
    '@
    
    # First_Name,Last_Name,DOB,City,Document_Type,Filename
    $csv2 = @'
    User1,Micah,11/01/1969,Parker,Transcript,T4IJZSYO.pdf
    User2,Acker,05/28/1960,,Transcript,R4IKTRYN.pdf
    User3,Aco,06/26/1950,,Transcript,R4IKTHMK.pdf
    User4,John,05/23/1960,,Letter,R4IKTHSL.pdf
    '@
    
    # hashtable
    $data = @{}
    
    $c1 = $csv1 -split "`r`n"
    $c1.count
    
    foreach ($item in $c1)
    {
        $fields = $item -split ','
        $key = $fields[1]+$fields[2]+$fields[3]
        $key
    
        # add new hashtable for given key
        $data.Add($key, [ordered]@{})
    
        # add data from c1 to the hashtable
        $data[$key].ID = $fields[0]
        $data[$key].First = $fields[1]
        $data[$key].Last = $fields[2]
        $data[$key].DOB = $fields[3]
    }
    
    $c2 = $csv2 -split "`r`n"
    $c2.count
    
    foreach ($item in $c2)
    {
        $fields = $item -split ','
        $key = $fields[0]+$fields[1]+$fields[2]
        $key
    
        # add data from c2 to the hashtable
        $data[$key].Type = $fields[4]
        $data[$key].FileName = $fields[5]
    }
    
    $data.Count
    
    foreach ($key in $data.Keys)
    {
        '====================='
        $data[$key]
    }
    

提交回复
热议问题