Search code examples
powershellcsvmergehashtableexport-to-csv

Merge two CSV files while adding new and overwriting existing entries


I have a configuration.csv that holds a template data like this:

| path       | item  | value  | type |
|------------|-------|--------|------|
| some/path  | item1 | value1 | ALL  |
| some/path  | item2 | UPDATE | ALL  |
| other/path | item1 | value2 | SOME |

and customization.csv that has service specific configuration:

| path       | item  | value  | type |
|------------|-------|--------|------|
| some/path  | item2 | value3 | ALL  |
| new/path   | item3 | value3 | SOME |

My goal is to merge them and end up with something like this:

| path       | item  | value  | type |
|------------|-------|--------|------|
| some/path  | item1 | value1 | ALL  |
| some/path  | item2 | value3 | ALL  |
| other/path | item1 | value2 | SOME |
| new/path   | item3 | value3 | SOME |

This should add any new entries and update any existing ones. No one column can be used for unique identification - both path and item needs to be combined, as they are guaranteed to be unique.


Solution

  • I suggest to use Compare-Object and as the values from customization.csv shall persist use this files values as -ReferenceObject

    ## Q:\Test\2019\03\01\SO_54948111.ps1
    
    $conf = Import-Csv '.\configuration.csv'
    $cust = Import-Csv '.\customization.csv'
    
    $NewData = Compare-Object -ref $cust -diff $conf -Property path,item -PassThru -IncludeEqual|
        Select-Object -Property * -ExcludeProperty SideIndicator
    
    $NewData
    $NewData |Export-Csv '.\NewData.csv' -NoTypeInformation
    

    Sample output

    > Q:\Test\2019\03\01\SO_54948111.ps1
    
    path       item  value  type
    ----       ----  -----  ----
    some/path  item2 value3 ALL
    some/path  item1 value1 ALL
    other/path item1 value2 SOME
    new/path   item3 value3 SOME