Windows Powershell: How to compare two CSV files and output the rows that are just in either of the file but not in both
Asked Answered
F

2

-1

I have a file first.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Alpha,Fan,120,jefferson,Riverside,NJ,8075

and second.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Bravo,Tan,220,Phila,Riverside,PA,9119

I want to compare the rows of both first.csv and second.csv files and output the rows that are either in first.csv or second.csv but not in both.

So the output.csv should have

Alpha,Fan,120,jefferson,Riverside,NJ,8075
Bravo,Tan,220,Phila,Riverside,PA,9119

There are quite a few similar questions but the output is not exactly what I want.

Thank you

Forseti answered 20/4, 2022 at 16:52 Comment(5)
Your question is unclear, what do you want to compare specifically between both CSVs ? And, what have you tried ?Salesperson
Thanks. I tried #23251297Forseti
Doesn't seem you want this question answered, please read stackoverflow.com/help/how-to-ask.Salesperson
That's quite helpful. I have edited the question. Hopefully, it's better nowForseti
Does this answer your question? In PowerShell, what's the best way to join two tables into one?Dissyllable
D
2
$filea = Import-Csv C:\Powershell\TestCSVs\group1.csv
$fileb = Import-Csv C:\Powershell\TestCSVs\group2.csv

Compare-Object $filea $fileb -Property name, surname, height, city, county, state, zipCode | Select-Object name, surname, height, city, county, state, zipCode | export-csv C:\Powershell\TestCSVs\out.csv -NoTypeInformation

I'm using the all the fields to compare and sort here but you can specify the unique value(s) that you're wanting to use to match the rows.

output

"name","surname","height","city","county","state","zipCode"     
"Bravo","Tan","220","Phila","Riverside","PA","9119"             
"Alpha","Fan","120","jefferson","Riverside","NJ","8075"
Dismissal answered 20/4, 2022 at 17:13 Comment(2)
Thanks. Not sure what I am missing but I tried your commands but the output CSV just has "name" text in it.Forseti
@Forseti try my editDismissal
B
0

Getting the symmetric difference (everything that is unrelated) from two lists is actually a quite common use in comparing objects. Therefore, I have added this feature (#30) to the Join-Object script/Join-Object Module (see also: In Powershell, what's the best way to join two tables into one?).

For this for this specific question:

PS C:\> Import-Csv .\First.csv |OuterJoin (Import-Csv .\Second.csv) |Format-Table

name  surname height city      county    state zipCode
----  ------- ------ ----      ------    ----- -------
Alpha Fan     120    jefferson Riverside NJ    8075
Bravo Tan     220    Phila     Riverside PA    9119
Bea answered 26/4, 2022 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.