Home > Mobile >  Filter two CSV with PowerShell
Filter two CSV with PowerShell

Time:06-22

$data1 = Import-Csv "SASSO1.csv" | ?{$_.SourceType -eq "file"} | select "FullPath"
$data2 = Import-Csv "SASSO2.csv" | ?{$_.SourceType -eq "file"} | select "FullPath"

$data1 | Where-Object -FilterScript { $_.FullPath -notin $data2 } | Export-Csv "RISULTATO.csv" -NoTypeInformation

My question is, how to find the difference between the two csv files, where it takes only one column or FullPath?

CodePudding user response:

You can use Compare-Object to make such comparisons.

Example

$data1 = @'
FullPath,Name,Desc
a,a,a
b,b,b
c,c,c
'@ | ConvertFrom-Csv

$data2 = @'
FullPath,Name,Desc
d,d,d,
e,e,e
c,c,c
'@ | ConvertFrom-Csv


Compare-Object -ReferenceObject $data1 -DifferenceObject $data2 -Property FullPath 

Results

FullPath SideIndicator
-------- -------------
d        =>
e        =>
a        <=
b        <=

By looking at the results, you can see that the record d and e are in $data2 only and a and b on $data1 only.

Based on that, you can export these record or do whatever you want with them. You can use the -PassThru switch if you want to get the complete objects along with the SideIndicator property so you get something like the following results

FullPath Name Desc SideIndicator
-------- ---- ---- -------------
d        d    d    =>
e        e    e    =>
a        a    a    <=
b        b    b    <=

That way, you can simply select the properties to export | Select-Object -Property FullPath,Name,Desc and export all the results that are not in both database.

You can also use the -IncludeEqual switch and possibly the -ExcludeDifferent switch if instead you want the results present in both csv to be included.

Finally, if you want just one side of the comparison (eg: only the records missing from $data1), then you can filter the results using a simple where statement (eg: | Where-Object -Property Sideindicator -eq '=>').

  • Related