0

I have two csv files with following columns

Csv1

Name| address| phone 
John| Blah | Blah 
Dave| Blah | Blah 
Rick| Blah | Blah

Csv2

Name |address| phone 
John| Blah| Blah 
Dave| Blah| Blah

I want unique rows from first csv

What i want

Name | address| phone 
Rick | Blah   | Blah

My code

$csv1 = import-csv <file> $csv2 = import-csv <file>
$output = $csv1 | where-object {$_.name -notin $csv2.Name -and $_.address -notin $csv2.address -and $_.phone -notin $csv2.phone}
0

1 Answer 1

2

Your statement | where-object {$_.name -notin $csv2.Name -and $_.address -notin $csv2.address -and $_.phone -notin $csv2.phone}

The logic when evaluating Rick's row

  • Is Rick a name that is in $Csv2 ? (Answer:No)
  • Is Blah an address in $Csv2 ? (Answer: Yes)
  • Is Blah a phone number in $Csv2 ? (Answer: Yes)

You are evaluating every single row against all of the rows of $csv2 so when you check Rick's address ($_.address -notin $csv2.address), it is actually in $Csv2 for John and Dave. Based on how you formulated your condition, that does eliminate Rick from the unique rows.

You can use Compare-Object to compare both csv files and find items that aren't a match. Here is a demonstration comparing both csv using the 3 properties as criterias.

Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2 -Property Name,Address,phone  | Where-Object SideIndicator -eq '<='

Complete self-contained example

$Csv1 = @'
Name|address|phone
John|Blah|Blah
Dave|Blah|Blah
Rick|Blah|Blah
'@ | ConvertFrom-Csv -Delimiter '|'


$csv2 = @'
Name|address|phone
John|Blah|Blah
Dave|Blah|Blah
'@ | ConvertFrom-Csv -Delimiter '|'


$Results = Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2 -Property Name,Address,phone  | Where-Object SideIndicator -eq '<='

$Results | Select Name,Address,Phone

Output

Name Address phone
---- ------- -----
Rick Blah    Blah
Sign up to request clarification or add additional context in comments.

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.