ripndip
ripndip

Reputation: 21

How to compare two .txt file strings in Powershell

I have 2 .txt files. txt file A has: | Dog | | Big Cat | | Fish | | Monkey | and txt file B has:

Monkey

| Monkey | | Big Cat | | Squirrel |

I am having a hard time trying to write a Powershell script that will take in these 2 .txt files and their contents, and comparing them in a conditional statement, and if file B has any of the same content of any line string as txt file A, then it'll output something to show there's a match.

This is my first programming question so I tried my best to explain my situation

Upvotes: 1

Views: 1731

Answers (1)

Daniel
Daniel

Reputation: 5114

Hopefully after some trying came out with something like this maybe?

$fileA = (Get-Content C:\temp\FileA.txt).Trim()
$fileB = (Get-Content C:\temp\FileB.txt).Trim()

Compare-Object $fileA $fileB -ExcludeDifferent -IncludeEqual  | Tee-Object -Variable compareObject
$compareObject | ForEach-Object -Begin { "`n" } -Process { "Found $($_.InputObject) in both files!" }

Output

InputObject SideIndicator
----------- -------------
Big Cat     ==
Monkey      ==


Found Big Cat in both files!
Found Monkey in both files!

Explanation

Assuming the pipes are not really part of the file and they separated onto their own lines

$fileA = @"
 Dog
Big Cat
Fish
Monkey
"@

$fileB = @"
Big Cat
Squirrel 
Monkey
"@

The following lines are getting the content of each file and placing them into string arrays. Then the .Trim() method is removing any whitespace from the beginning and ends of each item in the array

$fileA = (Get-Content C:\temp\FileA.txt).Trim()
$fileB = (Get-Content C:\temp\FileB.txt).Trim()

Next Compare-Object will compare the two arrays $fileA and $fileB. Normally Compare-Object will return only objects that are different and indicating which side they were found on

InputObject SideIndicator
----------- -------------
Squirrel    =>      # found only in $fileB
Dog         <=      # found only in $fileA
Fish        <=      # found only in $fileA

By adding the -ExcludeDifferent and -IncludeEqual tags we force Compare-Object instead to only return to use the values that it finds in both $fileA and $fileB

InputObject SideIndicator
----------- -------------
Big Cat     ==
Monkey      ==

Then we use this output anyway we like. I wanted to show the output of Compare-Object and do some additional tasks with it so I piped the output objects to Tee-Object -Variable somevariable. This will send the output to the variable and down the pipe which in this case is just out to the host/screen

Compare-Object $fileA $fileB -ExcludeDifferent -IncludeEqual | 
    Tee-Object -Variable compareObject  # set the $compareObject variable with results of Compare-Object 
                                        # and send down the pipe to host/screen

Last we loop through the 2 objects that Compare-Object gave us that it found equal in both arrays and formulate our message strings using the InputObject property (which as you can see above contains the values we are looking for)

$compareObject | ForEach-Object -Begin { "`n" } -Process { "Found $($_.InputObject) in both files!" }

Bonus

As a bonus I initially thought the OP wanted to compare the items in the lines including the pipes so I wrote this up. The only difference really is that I use regex to find the terms amongst all the whitespace and pipes, include all comparisons from Compare-Object, and don't output the extra strings at the end

$file1 = "| Dog | | Big Cat | | Fish | | Monkey | "
$file2 = "| Monkey | | Big Cat | | Squirrel |"

$matches1 = $file1 | Select-String -AllMatches  '\|\s?(\w*\s?\w*)\s?\|\s?'
$file1Terms = foreach ($match in ($matches1.Matches)){ $match.Groups[1].Value.Trim() }
$matches2 = $file2 | Select-String -AllMatches  '\|\s?(\w*\s?\w*)\s?\|\s?'
$file2Terms = foreach ($match in ($matches2.Matches)){ $match.Groups[1].Value.Trim()  }

Compare-Object $file1Terms $file2Terms -IncludeEqual

Output

InputObject SideIndicator
----------- -------------
Big Cat     ==
Monkey      ==
Squirrel    =>
Dog         <=
Fish        <=

Upvotes: 2

Related Questions