expirat001
expirat001

Reputation: 2195

Search unique values between 2 txt files

I have got 2 TXT files :

$file 1

water
bottle
train
car
bus

$file 2 

coffee
lamp
sport
train
book

I want to get the same value present in these 2 files, in that case the result expected is "train".

Upvotes: 1

Views: 100

Answers (3)

expirat001
expirat001

Reputation: 2195

I found another solution :

Compare-Object -ReferenceObject (gc .\file1.txt) -DifferenceObject (gc .\file2.txt) -IncludeEqual | Where-Object{$_.SideIndicator -eq "=="}

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36297

Alternatively you can use Compare-Object with the -IncludeEqual and -ExcludeDifferent switches ala:

Compare-Object (GC file1.txt) -Diff (gc file2.txt) -includeequal -excludedifferent

Add -Passthrough to the end and the only output you get is the value(s) you're looking for.

Upvotes: 1

mjolinor
mjolinor

Reputation: 68273

$file1 = Get-Content file1

Get-Content file2 | where { $file1 -contains $_ }

Upvotes: 0

Related Questions