Reputation: 137
I have two CSVs as following:
A
20180809000
20180809555
20180809666
20180809777
20180809888
File2:
A
20180809000
20180809555
20180809666
20180809777
I want to find difference of File1 - File2 which should output 20180809888
. I tried the following:
$a1= Import-Csv -Path $file1 | select A
$a2 = Import-Csv -Path $file2 | select A
$a1| where {$a2 -notcontains $_}
But it outputs the entire file 1:
A
--------------
20180809000
20180809555
20180809666
20180809777
20180809888
I tried intersection also, but that outputs null.
Upvotes: 0
Views: 289
Reputation:
The simplest solution is to use:
> Compare-Object (Get-Content .\File1.csv) (Get-Content .\File2.csv) -PassThru
20180809888
Or using Import-Csv
> Compare-Object (Import-Csv .\File1.csv).A (Import-Csv .\File2.csv).A -Passthru
20180809888
Or
> (Compare-Object (Import-Csv .\File1.csv) (Import-Csv .\File2.csv) -Passthru).A
20180809888
Upvotes: 3
Reputation: 3107
select A
will still return an object with a property named A
.
# Returns an object list with property A
Import-Csv -Path $file | select A # (shorthand for Select-Object -Property A)
# A
# ---
# value1
# value2
# ...
You can get the array of values of property A
using dot notation, e.g.:
# Returns the list of values of the A property
(Import-Csv -Path $file).A
# value1
# value2
# ...
The following should work:
$a1= (Import-Csv -Path $file1).A
$a2 = (Import-Csv -Path $file2).A
$a1 | where {$a2 -notcontains $_}
Upvotes: 1
Reputation: 25021
Your last line should be the following:
$a1.A.where{$_ -notin $a2.A}
To preserve the column, you can do the following for the last line:
$a1.where{$_.A -notin $a2.A}
The problem with this situation is that if the second file has more data than the first file. Then you would need to do something like this for your last line:
$a1 | compare $a2 | select -expand inputobject
Upvotes: 1