0

I have two csv files, one that contains a list of the MySQL 8.0 reserved keywords and another that contains four randomly chosen reserved keywords as well as test1, test2, test3 and test 4. I want to compare the two csv files, and return results that are present in both files. The test csv file contains first_value, rank, savepoint, shutdown, test1, test2, test3, and test4 screenshot of entries in csv file. For some reason, my script is only returning savepoint and shutdown instead of savepoint, shutdown, rank and first_value. I'm not sure why? I'd appreciate any thoughts!!

Results of script: you can see savepoint and shutdown are present in both in this image

    $array_list = @()
foreach($row in (Get-Content C:\ShowStopper\test3.csv)){
    $array_list += ($row.split(','))
}
$array_list

$array_list2 = @()
foreach($row in (Get-Content C:\ShowStopper\80.csv)){
    $array_list2 += ($row.split(','))
}
$array_list2

Compare-Object -ReferenceObject $array_list2 -DifferenceObject $array_list -IncludeEqual

I also tried a simpler variation, which did not work either.

$objects = @{
ReferenceObject = (Get-Content -Path C:\ShowStopper\test3.csv)
DifferenceObject = (Get-Content -Path C:\ShowStopper\80.csv)
}

Compare-Object @objects -IncludeEqual 
meredith
  • 1
  • 1
  • 2
    There are special cmdlets for the import (and export) of csv files. Surprisingly they are called [Import-CSV](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-5.1) and [Export-CSV](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-5.1). But watch out for the Headers. – T-Me Jun 16 '20 at 15:09
  • Thank you for your response! I tried changing it to use Import-CSV, and I'm still only receiving savepoint and shutdown as equal results... `$tablenames = Import-CSV -Path "C:\ShowStopper\test3.csv" | Select -ExpandProperty 'Table Name' $tablenames $keywords = Import-CSV "C:\ShowStopper\80.csv" | Select -ExpandProperty 'Table Name' $keywords Compare-Object -ReferenceObject $tablenames -DifferenceObject $keywords -IncludeEqual -ExcludeDifferent` is the updated code I tried. – meredith Jun 16 '20 at 15:52
  • 1
    If you want to compare an array of objects, remove the `-ExpandProperty`. If you want to compare an array of strings, see: [Compare two arrays](https://stackoverflow.com/a/35872835/1701026) – iRon Jun 16 '20 at 18:17
  • Are you sure there isn't a hidden whitespace or something like that in one of the CSVs? Maybe try an other tool to compare the CSVs manually. E.g. Notepad++ with the compare addon to visualize the matches – T-Me Jun 17 '20 at 06:24
  • @T-Me thank you for that idea, another coworker tried the various codes as well and that's the conclusion he has reached also.. working now to identify the culprit. Thank you! – meredith Jun 17 '20 at 16:31

0 Answers0