0
votes

I have 2 CSV files with same headings, i need to compare both and remove duplicates from one file. for example

CSV1:

RollNo  Class   Name   School
100      X      Raja    XYZ
101      Y      Krish   XYZ
102      A      Joe     ABC

CSV2:

RollNo  Class   Name   School
200       B     Puja   XYZ
100       X     Raja   XYZ
201       B     Jery   ABC

The output expecing is as below (only From CSV1)

RollNo  Class  Name  School
101      Y     Krish   XYZ
102      A     Joe     ABC

Can somebody put a light in to it. I was trying with Compare-Object, but it confuses me a lot.

Compare-Object $csv1 $csv2 -Property RollNo,Class,Name,School -
    IncludeEqual -ExcludeDifferent |select * -ExcludeProperty 
    SideIndicator | Export-Csv School.csv -NoTypeInfo
1
The PowerShell code you posted is broken. Please post the code exactly the way you have it. Do not arbitrarily wrap lines. Also, please post the raw CSV data (open the files with a text editor and copy/paste the content).Ansgar Wiechers

1 Answers

1
votes

Compare-Object is defenitly the way to go. What you have missed (or not shown) is to convert the csv to an object using Import-Csv before.

$c1 = Import-Csv csv1.csv
$c2 = Import-Csv csv2.csv
Compare-Object $c1 $c2 |?{ $_.SideIndicator -eq '<=' } | select -expand inputobject | Export-Csv csv3.csv -NoTypeInformation