0

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.

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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 $_}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.