0

I'm using Compare-Object in PowerShell to compare two XML files. It adequately displays the differences between the two using <= and =>. My problem is that I want to see the difference in context. Since it's XML, one line, one node, is different, but I don't know where that lives in the overall document. If I could grab say 5 lines before and 3 lines after it, it would give me enough information to understand what it is in context. Any ideas?

2 Answers 2

1
You can start from something like this:

$a = gc a.xml
$b = gc b.xml

if ($a.Length -ne $b.Length)
    { "File lenght is different" }
    else
    {
    for ( $i= 0; $i -le $a.Length; $i++)    
        {
            If ( $a[$i] -notmatch $b[$i] ) 
            {
             #for more context change the range i.e.: -2..2       
             -1..1 | % { "Line number {0}: value in file a is {1} - value in file b {2}" -f ($i+$_),$a[$i+$_], $b[$i+$_] }
                " "
            }  
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm.... I'll try that out. I was heading down this road, but thought if $a is missing a line or two, then everything wouldn't match after it right? However, I don't know how compare-object handles that.
Yeah, I removed a line and the entire rest of the doc didn't match. Hmmm.. I could iterate through each collection independently looking for matching tags first, then content.
Scratch that, I need to treat it like an XML file, and query and find the differences. Plain text comparison I don't think will cut it (what if a data element becomes out of order?). Treating it like a data object should resolve a lot of the unknowns. Thanks for the help
@DavidLozzi Yes, if you can compare xml as xml-object is more accurate. But if the file are just some little difference is some line my code can work great, IMHO ;)
0

Compare-Object comes with an IncludeEqual parameter that might give what you are looking for:

[xml]$aa = "<this>
 <they>1</they>
 <they>2></they>
 </this>"
[xml]$bb = "<this>
 <they>1</they>
 <they>2</they>
 </this>"
Compare-Object $aa.this.they $bb.this.they -IncludeEqual

Result

InputObject SideIndicator
----------- -------------
1           ==
2           =>
2>          <=

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.