header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Ignoring sections when using Compare-Object
Last Post 25 Aug 2010 08:45 AM by harrier. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
harrierUser is Offline
New Member
New Member
Posts:6
Avatar

--
24 Aug 2010 02:48 PM
    I'm trying to compare two files but ignore a segment of each file.

    eg.

    There is a section of each file that starts with "logged"

    and ends with "/logged" with data inbetween


    I don't want to compare anything in that section.

    Hints on how to do this? (Just started new job and handed this first day. Never touched powershell so trying to learn as quickly as possible. Last person quit suddenly and I want to help get this going)
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    24 Aug 2010 04:20 PM

    FYI: Your post appears to have some missing information.

    Are these marks on the each line that needs to be filtered out?

    harrierUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    24 Aug 2010 04:23 PM
    Oh scrud, it does. Thanks

    so the sections that need to be filtered out fall between a
    (logged)
    and
    (/logged)

    Carats instead of parens
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    24 Aug 2010 04:30 PM
    Are these markers always on lines by themselves? Are these files XML?
    harrierUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    24 Aug 2010 04:35 PM
    Yes, I'm sorry. I'm not trying to make you pull teeth here.

    They are xml. The identifiers are on lines by themselves. I'd paste a section but it's all greater than/less than so it likely wouldn't help. The main body of the file changing is what we are concerned with, not if the client changes how it is logged (eg. debug, warn, all).
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    24 Aug 2010 04:41 PM

    No worries. I'm just trying to get enough information so that I can create a prototype solution for you. Give me a few minutes to try a few things.

    harrierUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    24 Aug 2010 04:48 PM
    Thanks! If I don't reply immediately, I will tomorrow early.
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    24 Aug 2010 05:02 PM

    Try something like this:

    function Read-XmlFile { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateScript({Test-Path -Path $_})] [string] $Path ) $doc = New-Object -TypeName System.Xml.XmlDocument $doc.set_PreserveWhitespace($true) $doc.Load($Path) $doc.SelectNodes('//logged') | ForEach-Object -Process { $parent = $_.get_ParentNode() $null = $parent.RemoveChild($_) } $doc.get_OuterXml() -split [System.Environment]::NewLine } $refFile = 'C:\temp\file1.xml' $difFile = 'C:\temp\file2.xml' $refDoc = Read-XmlFile -Path $refFile $difDoc = Read-XmlFile -Path $difFile Compare-Object -ReferenceObject $refDoc -DifferenceObject $difDoc

    Be aware that Compare-Object will only give you line by line differences rather than perform a proper XML diff that follows XML rules (like ignoring whitespace, etc.).

    harrierUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    25 Aug 2010 08:45 AM
    Fantastic! That works great and would have taken me.. probably forever.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer