Find the content-difference in files irrespective of blank lines and spaces.

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of David Johnson David Johnson 1 year ago.

  • Author
  • #30169
    Profile photo of Shelly Tomar
    Shelly Tomar

    Here I am able to compare 2 web.configs such that spaces are ignored while comparison. But I want to find a way to ignore blank lines too during comparison. How should I do it?
    path1-> path of folder containing web.config files to compare with those of path2
    path2-> path of folder containing web.config files to compare with those of path1

    function Diff_Content{
    $count1=(Get-ChildItem -Path $path1 -Filter *.config).count
    $count2=(Get-ChildItem -Path $path2 -Filter *.config).count
    if ($count1 -ne $count2)
    Write-Host("Number of web.config files is different !")
    $folder1=Get-ChildItem -Path $path1 -Filter *.config
    $folder2=Get-ChildItem -Path $path2 -Filter *.config
    Compare-Object -ReferenceObject $folder1 -DifferenceObject $folder2
    while($count1 -gt 0)
    $path3=(Get-ChildItem -Path $path1 -Filter *.config)[$i].FullName
    $path4=(Get-ChildItem -Path $path2 -Filter *.config)[$i].FullName
    $a = (sls '^' -Path $path3) -replace '.:[^:]+:(.+)','$1' -replace " " 
    $b = (sls '^' -Path $path4) -replace '.:[^:]+:(.+)','$1' -replace " " 
    $file1 = Split-Path $path3 -Leaf
    $file2 =Split-Path $path4 -Leaf
    $result = switch (diff $b $a ){
    	{ $_.sideindicator -eq '=>' } { $file1 + $_.inputobject }
    	{ $_.sideindicator -eq '< =' } { $file2 + $_.inputobject }
  • #30517
    Profile photo of Don Jones
    Don Jones

    If you're using a regex, /s represents all whitespace, and /S represents all non-whitespace. Could you simply replace all non-whitespace with an empty string ("") to get rid of it?

  • #30591
    Profile photo of David Johnson
    David Johnson

    Getting it to work with Regex is surprisingly difficult.
    I was convinced something like this would work:

    [Regex]::Replace((Get-Content $filename -Raw),"^\s*$",'',[System.Text.RegularExpressions.RegexOptions]::Multiline)

    But I can't get it to.

    Select-String seems a little better at handling it. Change the pattern lines to:

    $a = Select-String -Pattern "^\S+$" -path $file1|Select -ExpandProperty Line

    And it should work. You won't need all the -Replace gubbins anymore either, the Select statement takes care of that for you.

You must be logged in to reply to this topic.