Forum Discussion

VinayNaguru's avatar
VinayNaguru
Copper Contributor
Sep 08, 2022

Need to compare only specific lines

Hi, Can someone help to write a script to compare only few required lines between configuration files. For example if file1 has key value pair stating abc=123, it should compare with another file file2 and show the result if there is any changes in that specified line.

 

In my configuration file there will hundreds of lines. So those lines should be memorized first for easy comparing.

12 Replies

  • Could you share a part of the configuration file if possible and what do you have so far as a script?
    • VinayNaguru's avatar
      VinayNaguru
      Copper Contributor

      Hi Harm_Veenstra,

       

      Sure. Below is the part of the script. Script need to identify only the lines which doesn't have '#' and highlighted in red color (there is no color code in original file. Highlighted by me for easy identification)

       

      # This parameter defines whether to use CORS filter on Auth server security filter chain.
      # This parameter can only be set during initialization.
      #
      # Default=true
      #
      # auth.enable.corsfilter=true
      auth.enable.corsfilter=true

      # This parameter defines the number of days that a user can keep a password before it is expired
      # This parameter can only be set during initialization.
      #
      # Default=60
      #
      # auth.password.expiration.days=60
      auth.password.expiration.days=60

      # This parameter defines the number of previous passwords that cannot be used for a new password.
      # This parameter can only be set during initialization.
      #
      # Default=1
      #
      # auth.password.history.size=1
      auth.password.history.size=1
       
       

       

      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP

        VinayNaguru I made a very simple compare 

         

        $file1 = get-content .\file1.txt
        $file2 = get-content .\file2.txt
        $contentsfile1 = @()
        
        foreach ($line in $file1) {
            if ($line.StartsWith('#')) {
                #Skip
            }
            else {
                if ($line.Contains('=')) {
                    $contentsfile1 += $line
                }
            }
        }
        
        foreach ($value in $contentsfile1) {
            if (-not ($file2 | Select-String $value)) {
                $found = $file2 | Select-String $value.Split('=')[0] | Select-String -Pattern '#' -NotMatch
                write-host ("{0} not found, value found is {1}" -f $value, $found) -ForegroundColor Red
            
            }
        }

         

        Contents file 1

        # This parameter defines whether to use CORS filter on Auth server security filter chain.
        # This parameter can only be set during initialization.
        #
        # Default=true
        #
        # auth.enable.corsfilter=true
        auth.enable.corsfilter=false
        
        # This parameter defines the number of days that a user can keep a password before it is expired
        # This parameter can only be set during initialization.
        #
        # Default=60
        #
        # auth.password.expiration.days=60
        auth.password.expiration.days=30
        
        # This parameter defines the number of previous passwords that cannot be used for a new password.
        # This parameter can only be set during initialization.
        #
        # Default=1
        #
        # auth.password.history.size=1
        auth.password.history.size=2

        Contents File 2

        # This parameter defines whether to use CORS filter on Auth server security filter chain.
        # This parameter can only be set during initialization.
        #
        # Default=true
        #
        # auth.enable.corsfilter=true
        auth.enable.corsfilter=true
        
        # This parameter defines the number of days that a user can keep a password before it is expired
        # This parameter can only be set during initialization.
        #
        # Default=60
        #
        # auth.password.expiration.days=60
        auth.password.expiration.days=60
        
        # This parameter defines the number of previous passwords that cannot be used for a new password.
        # This parameter can only be set during initialization.
        #
        # Default=1
        #
        # auth.password.history.size=1
        auth.password.history.size=1

         

        Output on screen:
        auth.enable.corsfilter=false not found, value found is auth.enable.corsfilter=true
        auth.password.expiration.days=30 not found, value found is auth.password.expiration.days=60
        auth.password.history.size=2 not found, value found is auth.password.history.size=1