Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Comparing files with compare-object

Reply
 
Old 07-06-2007   #1 (permalink)
Marco Shaw


 
 

Comparing files with compare-object

I've done everything I can think of to properly format a string to
compare between 2 files.

I've confirmed to my best of knowledge that one particular entry is
*exactly* the same in two files (same lenght, etc). However, I cannot
seem to get compare-object to find a match.

76# import-csv file1.csv|%{$_.'SN,CN'}|%{$_.replace(' (+)','')}|%{if($_
-match "some_user_removed"){$_}}|%{$_.length}
24
24
24
24
24
24
24
24
24
24
24
77# get-content file2.txt|%{$_.substring(13,56)}|%{$_.trimend()}|%{if($_
-match "some_user_removed"){$_}}|%{$_.length}
24

So above, I've confirmed (I think) that the string is exactly the same
lenght, however I can't get compare-object to indicate a match. One
file has 9,000 entries (lines), and the other has over 63,000.

I'm trying to get *only* the entries that match exactly between each
file. Something like this:

79# compare-object -ReferenceObject $(gc 1.txt) -DifferenceObject $(gc
2.txt) -includeequal -excludedifferent

InputObject SideIndicator
----------- -------------
test ==


80# gc 1.txt
test
testing
81# gc 2.txt
test

Any ideas? Gotchas I might be missing?

My System SpecsSystem Spec
Old 07-06-2007   #2 (permalink)
Marco Shaw


 
 

Re: Comparing files with compare-object

More info...

If I try to do an exact match for the -referenceobject and
-differenceobject, I can get a match.

This works:

85# compare-object -referenceobject $(get-content
1.txt|%{$_.substring(13,56)}|%{$_.trimend()}|%{if($_ -mat
ch "some_user_removed"){$_}}) -differenceobject $(import-csv
2.csv|%{$_.'SN,CN'}|%{$_.replace(' (+)','')}|%
{if($_ -match "some_user_removed"){$_}}) -includeequal -excludedifferent

InputObject SideIndicator
----------- -------------
some_user_removed ==

But again, this doesn't:

86# compare-object -referenceobject $(get-content
1.txt|%{$_.substring(13,56)}|%{$_.trimend()}) -difference
object $(import-csv 2.csv|%{$_.'SN,CN'}|%{$_.replace(' (+)','')})
-includeequal -excludedifferent
87#

My understanding is compare-object is not supposed to be a match line X
in file A with match line X in file B. In other words, line X in file A
can still be found to be a match with line Y in file B as long as the
content/string is the same.
My System SpecsSystem Spec
Old 07-06-2007   #3 (permalink)
Marco Shaw


 
 

Re: Comparing files with compare-object


> Any ideas? Gotchas I might be missing?


I've found a workaround. It ain't pretty, but seems to work...
My System SpecsSystem Spec
Old 07-06-2007   #4 (permalink)
Keith Hill


 
 

Re: Comparing files with compare-object

"Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:OjyUUr8vHHA.1756@TK2MSFTNGP05.phx.gbl...
> I've done everything I can think of to properly format a string to compare
> between 2 files.
>


Did you try setting the -SyncWindow to something larger like 100?

--
Keith

My System SpecsSystem Spec
Old 07-06-2007   #5 (permalink)
Marco Shaw


 
 

Re: Comparing files with compare-object

Keith Hill wrote:
> "Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
> news:OjyUUr8vHHA.1756@TK2MSFTNGP05.phx.gbl...
>> I've done everything I can think of to properly format a string to
>> compare between 2 files.
>>

>
> Did you try setting the -SyncWindow to something larger like 100?
>
> --
> Keith


No I didn't! That has to be the key... I'm comparing a 8,000 line file
to a 63,000 one...
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: ignore whitespace when comparing files using compare-object? PowerShell
ignore whitespace when comparing files using compare-object? PowerShell
Re: ignore whitespace when comparing files using compare-object? PowerShell
Testing object arrays using Compare-Object and -contains PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46