-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

iRon
  • 20,463
  • 10
  • 53
  • 79
polapts
  • 5,493
  • 10
  • 37
  • 49
  • 1
    Your question is unclear, what do you want to compare specifically between both CSVs ? And, what have you tried ? – Santiago Squarzon Apr 20 '22 at 16:53
  • Thanks. I tried https://stackoverflow.com/questions/23250797/compare-two-csv-files-compare-the-difference-and-write-difference-in-csv-file – polapts Apr 20 '22 at 17:01
  • 1
    Doesn't seem you want this question answered, please read https://stackoverflow.com/help/how-to-ask. – Santiago Squarzon Apr 20 '22 at 17:02
  • That's quite helpful. I have edited the question. Hopefully, it's better now – polapts Apr 20 '22 at 17:16
  • Does this answer your question? [In PowerShell, what's the best way to join two tables into one?](https://stackoverflow.com/questions/1848821/in-powershell-whats-the-best-way-to-join-two-tables-into-one) – TylerH May 23 '22 at 13:17

2 Answers2

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"
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
iRon
  • 20,463
  • 10
  • 53
  • 79