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
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+$_] }
" "
}
}
}
4 Comments
David Lozzi
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.
David Lozzi
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.
David Lozzi
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
CB.
@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 ;)
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> <=