Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell CSV Merger / Comparison

$
0
0

Hi,

I'm trying to perform a merge of data between multiple CSVs in multiple ways all based on a single similar property. Finding the data to be merged hasn't been an issue but combining it has so far.

First I'll try to give an overview of what I'm working to accomplish in this particular case:

CSV1.csv

Name,Attrib1,Attrib2
Object1,111,True
Object2,222,False

CSV2.csv

Name
Object5
Object6

CSV3.csv

Name,Attrib1,AttribB
Object2,222,YYY
Object3,333,ZZZ

Final Output:

Name,Attrib1,Attrib2,AttribB
Object1,111,True,
Object2,222,False,YYY
Object3,333,,ZZZ
Object5,,,
Object6,,,


I load each CSV into an array, and I'm currently working with the idea of
using Compare-Object to solve the issue with objects needing to be added
to the first array even if they're not present in it, but still combining
rows based on a common value. I've been able to gather the differences but
not extract the rows from the second array to add to the first.

This is what I'm using for the differences portion from 1 to 2:

$1 = Import-Csv csv1.csv
$2 = Import-Csv csv2.csv

$1n = $1 | % {$_.Name}
$2n = $2 | % {$_.Name}

$NameDiff = Compare-Object $1n $2n -PassThru

This successfully gives me the list of items in csv2 that aren't in csv1. I've also
worked with Join-Object to try splicing everything together, but I haven't gotten that
quite right yet as it only seems to give rows where both CSVs have the value in the
column I'm using so far. I'm thinking it's in the way I'm using the Where param but I
can't seem to find a way to resolve it. Here's the code I'm trying at the moment
(using the Join-Object cmdlet):

$join1 = Join-Object -Left $1 -Right $3
    -LeftProperties Name,Attrib1,Attrib2
    -RightProperties AttribB
    -Where { $args[0].Name -eq $args[1].Name }

This works, but if the "Name" isn't in both, it obviously won't be in the resulting

file, which I need it to be.

~Brandit










Viewing all articles
Browse latest Browse all 15028

Trending Articles