1

I have two text files, one named “master.txt” and other “slave.txt”. Each text files contains a set of users as below, and I wanted to compare master.txt file with slave.txt file and report the missing ones in the slave.txt file. In the current result, it seems both the files are compared and responds back missing ones across both the files. Could someone help me please?

~]# cat master.txt
user1
user2
user3

~]# cat slave.txt
user2
user4

Script:

$mfile = Get-Content "C:\master.txt"
$sfile = Get-Content "C:\slave.txt" 
foreach ($mobj in $mfile) {

foreach($sobj in $sfile){
        if ($mobj -ne $sobj) {
        Write-Output "$mobj is Not Found:"
        }
     }
}

Current result:

user1 is Not Found:
user1 is Not Found:
user2 is Not Found:
user3 is Not Found:
user3 is Not Found:

Expected result (these are the ones present in master.txt but not in slave.txt):

user1 is Not Found:
user3 is Not Found:
user3721640
  • 777
  • 2
  • 11
  • 15
  • Switch to compare for equality and do not print when equal then continue with next item. Only print "not found" if not found! What you are doing is print not found when it is actually itmes not equal. – Fildor Nov 16 '15 at 14:16

1 Answers1

3

Compare is a better option than the diff

Here's a sample code, the slide indicator displays the missing data in each file

$a = Get-Content .\master.txt
$b = Get-Content .\slave.txt
Compare-Object $a $b

Output

InputObject SideIndicator
----------- -------------
user4       =>
user1       <=
user3       <=
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15