0

I have this one section of my PowerShell script that I'm currently stuck on. Basically, I have two files that I want to do a comparison on via select-string...In detail

For each item in FileA.txt I want to do a select-string to FileB.txt to discover if it exist. If the line item in FileA.txt doesn't exist in FileB.txt, then print the FileA.txt line item to the screen.

This is what the text files looks like..more or less

FileA.txt

1
2
3
4
6

FileB.txt

6
7
8
9
10

Desired output would be the following:

1
2
3
4

This is what my PS code looks like now. My thought process was that I could use the variable within the select-string but its not working out for me :(

$IPs = Get-Content "C:\\FileA.txt" 

Get-Content C:\FileB.txt | Select-String -InputObject $IPs

Could someone please help me out and point out what I am doing wrong.

briantist
  • 45,546
  • 6
  • 82
  • 127

2 Answers2

2

Based on your limited sample data, here is an example of how you could do this :

"1 2 3 4 6" > "fileA.txt"
"6 7 8 9 10" > "fileB.txt"

$arrayA = (Get-Content "fileA.txt").Split(" ")
$arrayB = (Get-Content "fileB.txt").Split(" ")

$arrayResult = @()

foreach($valueA in $arrayA) {
    if($arrayB -notcontains $valueA) {
        $arrayResult += $valueA
    }
}

$arrayResult -join " "

Now I believe the input files will be quite different eventually

EDIT :

Using line breaks :

"1
2
3
4
6" > "fileA.txt"

"6
7
8
9
10" > "fileB.txt"

$arrayA = Get-Content "fileA.txt"
$arrayB = Get-Content "fileB.txt"

$arrayResult = @()

foreach($valueA in $arrayA) {
    if($arrayB -notcontains $valueA) {
        $arrayResult += $valueA
    }
}

$arrayResult -join "`n"

NB : the 2 scripts begin by filling the needed files, I guess you won't need to do it

sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • I just edited his question, it looked like he meant to have each value in the file be on its own line, it just didn't show up that way in the post (his line breaks weren't being interpreted). – briantist Sep 18 '15 at 16:29
  • Edited my answer to reflect this :) – sodawillow Sep 18 '15 at 16:32
  • `Get-Content` returns an array of lines by default, so no need to `.Split()` anymore, but otherwise looks good, +1 – briantist Sep 18 '15 at 16:36
  • I felt I was doing this wrong somehow, you are right / initial sample data got me doing this : ) and thanks – sodawillow Sep 18 '15 at 16:36
  • *If you wish so, you can accept the answer by clicking on the check on the left near the votes* – sodawillow Sep 18 '15 at 16:47
  • Thanks everyone. Your responses were great. The code from sodawillow worked they way I wanted once I removed "1 2 3 4 6" > "fileA.txt" "6 7 8 9 10" > "fileB.txt" – Tech_Spaniard Sep 18 '15 at 16:53
2

In this specific example, Compare-Object would probably be a better choice. It is designed to find the differences between two lists.

You would use something like:

Compare-Object -ReferenceObject $(gc .\FileA.txt) -DifferenceObject $(gc .\FileB.txt) | where { $_.SideIndicator -eq '<=' } | select -expand InputObject

However, you could also do this with select-string:

gc .\FileA.txt | select-string -Pattern $(gc .\FileB.txt) -NotMatch

which just finds the lines of FileA that don't match the lines of FileB, however the lines of FileB are interpreted as regular expressions, which probably isn't appropriate for IP addresses since '.' is a wildcard.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • I tried very quickly with `Compare-Object` but didn't know it well enough to post an answer with it :) looks cleaner, but that's maybe because of the one-liner/pipeline style ^^ – sodawillow Sep 18 '15 at 17:46