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 - How can you find the lines Compare-Object determines to be differe

Reply
 
Old 10-23-2007   #1 (permalink)
Bob Landau


 
 

How can you find the lines Compare-Object determines to be differe

Regardless of whether Compare-Object is comparing two text files or the
"chosen" properites of two collections: Files/Processes/... ; there is a
notion of a line or index where the difference was found.

Unfortunately I've not been able to find out how to make Compare-Object do
this.

I thought on a lark I could do pipe the outputs and track the differences
myself however the line counts are ALL messed up

The below will illustrate what I'm seeing

## file Out1; copy OUt1 to Out2 and change one character
clock.avi
imsins.BAK
Blue Lace 16.bmp
Coffee Bean.bmp

## this just dumps out the "line number" and file for debugging purposes
Compare-Object ${cut1} ${cut2} -includeequal | % {$i = 0} { $i++; echo
"`$i is $i and `$_ is $_`n"}


What I've found are the lines that are _equal_ are feed to the pipe first.
Only after this has been drained are the differences piped out. My guess is
Compare-Object is using two arrays (Equal/Diff) and then dumping them to the
pipe.

So now I'm back to square one. Does someone know whether Compare-Object can
dump the "line" numbers out? If not has someone written a Comparsion tool
which will dump out what index/line in the Array/file the differences are
occuring.

Seems like it would have been such a simple feature to add :<

My System SpecsSystem Spec
Old 10-23-2007   #2 (permalink)
Kiron


 
 

Re: How can you find the lines Compare-Object determines to be differe

Here is a way with Where-Object and the containment operators. Pipe the line numbers to where-object
so it filters each different line and outputs its number:

1..${cut1}.length | ? {${cut2} -notContains ${cut1}[$_ - 1]}

# ...for the equal lines
1..${cut1}.length | ? {${cut2} -contains ${cut1}[$_ - 1]}

--
Kiron
My System SpecsSystem Spec
Old 10-24-2007   #3 (permalink)
Jacques Barathon [MS]


 
 

Re: How can you find the lines Compare-Object determines to be differe

"Kiron" <Kiron@xxxxxx> wrote in message
news:82172493-D903-40C0-853C-D9A41A0912E3@xxxxxx
Quote:

> Here is a way with Where-Object and the containment operators. Pipe the
> line numbers to where-object
> so it filters each different line and outputs its number:
>
> 1..${cut1}.length | ? {${cut2} -notContains ${cut1}[$_ - 1]}
>
> # ...for the equal lines
> 1..${cut1}.length | ? {${cut2} -contains ${cut1}[$_ - 1]}
If you need something that scales up well with large text files, you can try
a script I posted about a week ago that does exactly what you want:

http://janel.spaces.live.com/blog/cn...88C2!345.entry

The blog is in French but you should be able to copy/paste the script
(compare-textfile.ps1) and use it straight away. Let me know however if you
have any question.

The script was not tested thoroughly and is provided "as is" with no
guarantee of any sort.

Jacques

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
compare-object PowerShell
compare object PowerShell
Compare-Object and Get the name of object/File? 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