Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compare two text files and report differences in a text file format

680 views
Skip to first unread message

Larry__Weiss

unread,
Jan 12, 2010, 6:27:45 PM1/12/10
to
I thought that perhaps that Compare-Object could be used as the basis of a
script that takes two text files and creates a text file documenting
differences, including the line numbers of the differing lines and the content
of those lines. But I'm frustrated that I can't get past just outputting the
content of the lines, and not the line numbers of differing content.

Should I give up on Compare-Object or am I overlooking something?

- Larry

Karl Mitschke

unread,
Jan 13, 2010, 10:37:53 AM1/13/10
to
Hello Larry,

larry;

Try this:

$dif = Compare-Object -ReferenceObject $(Get-Content test.txt) -DifferenceObject
$(Get-Content test2.txt) -IncludeEqual
$lineNumber = 1
foreach ($difference in $dif)
{
if ($difference.SideIndicator -ne "==")
{
Write-Output "Line Number $linenumber is different"
}
else
{
Write-Output "Line Number $linenumber is equal"
}
$lineNumber ++
}

Karl
http://unlockpowershell.wordpress.com/


Bob Landau

unread,
Jan 13, 2010, 10:56:01 AM1/13/10
to
Compare-Object in my opinion should be explicitly stated in the doc's that is
not for text files but for PSObjects. What you mentioned below and no ability
to overlook whitespace are real problems for me when it comes to file
comparison.

You may have more patience than me but I gave up using this cmdlet on text
files long ago.

As to what to use Findstr, WinDiff come to mind both from Microsoft and Diff
(you should be able to find a BSD version).

"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Jan 13, 2010, 11:21:39 AM1/13/10
to
Can WinDiff be automated via a PowerShell script ?

- Larry

Karl Mitschke

unread,
Jan 13, 2010, 11:51:36 AM1/13/10
to

PaulChavez

unread,
Jan 13, 2010, 12:41:04 PM1/13/10
to

One method you can try is to take the output of get-content and create a
custom object that has line number and line text properties. Then when using
compare object you can reference the line number.

Here's a simple example:

PS>"alpha","bravo","charlie" | set-content ABC.txt
PS>"charlie","delta","echo" | set-content CDE.txt
PS>$abc = gc .\ABC.txt | %{$i = 1} { new-object psobject -prop
@{LineNum=$i;Text=$_}; $i++}
PS>$cde = gc .\CDE.txt | %{$i = 1} { new-object psobject -prop
@{LineNum=$i;Text=$_}; $i++}
PS>Compare-Object $abc $cde -Property Text -PassThru -IncludeEqual
Text LineNum SideIndicator
---- ------- -------------
charlie 3 ==
delta 2 =>
echo 3 =>
alpha 1 <=
bravo 2 <=

Hope that helps,
-Paul

"Larry__Weiss" wrote:

> .
>

PaulChavez

unread,
Jan 13, 2010, 1:08:02 PM1/13/10
to
Hmm first reply was lost in the ether I guess.

You can do this with compare-object if you use the data from get-content to
create a new custom object with the line number. Here is an example:

> "alpha","bravo","charlie" | set-content ABC.txt

> "charlie","delta","echo" | set-content CDE.txt

> $abc = gc .\ABC.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}

> $cde = gc .\CDE.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}

> Compare-Object $abc $cde -Property Text -PassThru -IncludeEqual

Text LineNum SideIndicator
---- ------- -------------
charlie 3 ==
delta 2 =>
echo 3 =>
alpha 1 <=
bravo 2 <=


Hope that helps,
Paul

"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Jan 13, 2010, 1:13:08 PM1/13/10
to
It does help!

I forget how much of PowerShell's "power" I leave untouched when I design a
solution.

- Larry

0 new messages