Reputation: 33
I have seen powershell script which also I have in mind. What I would like to add though is another column which would show the side indicator comparators ("==", "<=", "=>") and be named them as MATCH(if "==") and MISMATCH(if "<=" and "=>"). Any advise on how I would do this?
Here is the link of the script (Credits to Florent Courtay) How can i reorganise powershell's compare-object output?
$a = Compare-Object (Import-Csv 'C:\temp\f1.csv') (Import-Csv 'C:\temp\f2.csv') -property Header,Value
$a | Group-Object -Property Header | % { New-Object -TypeName psobject -Property @{Header=$_.name;newValue=$_.group[0].Value;oldValue=$_.group[1].Value}}
========================================================================
The output I have in mind:
Header1 Old Value New Value STATUS
------ --------- --------- -----------
String1 Value 1 Value 2 MATCH
String2 Value 3 Value 4 MATCH
String3 NA Value 5 MISMATCH
String4 Value 6 NA MISMATCH
Upvotes: 3
Views: 828
Reputation: 437278
Here's a self-contained solution; simply replace the ConvertFrom-Csv
calls with your Import-Csv
calls:
# Sample CSV input.
$csv1 = @'
Header,Value
a,1
b,2
c,3
'@
$csv2 = @'
Header,Value
a,1a
b,2
d,4
'@
Compare-Object (ConvertFrom-Csv $csv1) (ConvertFrom-Csv $csv2) -Property Header, Value |
Group-Object Header | Sort-Object Name | ForEach-Object {
$newValIndex, $oldValIndex = ((1, 0), (0, 1))[$_.Group[0].SideIndicator -eq '=>']
[pscustomobject] @{
Header = $_.Name
OldValue = ('NA', $_.Group[$oldValIndex].Value)[$null -ne $_.Group[$oldValIndex].Value]
NewValue = ('NA', $_.Group[$newValIndex].Value)[$null -ne $_.Group[$newValIndex].Value]
Status = ('MISMATCH', 'MATCH')[$_.Group.Count -gt 1]
}
}
The above yields:
Header OldValue NewValue Status
------ -------- -------- ------
a 1 1a MATCH
c 3 NA MISMATCH
d NA 4 MISMATCH
Note:
The assumption is that a given Header
column value appears at most once in each input file.
The Sort-Object Name
call is needed to sort the output by Header
valuesThanks, LotPings.
, because, due to how Compare-Object
orders its output (right-side-only items first), the order of groups created by Group-Object
would not automatically reflect the 1st CSV's order of header values (d
would appear before c
).
Upvotes: 5