Forum Discussion
Need to compare only specific lines
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)
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=2Contents 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
- VinayNaguruSep 08, 2022Copper Contributor
- Sep 08, 2022Let us know the results 🙂
- VinayNaguruSep 10, 2022Copper ContributorIt worked for text and config files. But not working for the Yaml file. So it will be different for it?
- LainRobertsonSep 08, 2022Silver Contributor
I went for something a little more complex. May be overkill for this scenario but here it is anyway.
function Get-FileDifferences([string]$SourceFile, [string]$TargetFile) { if ((Test-Path -Path $SourceFile) -and (Test-Path -Path $TargetFile)) { $TargetHashes = [System.Collections.Generic.Dictionary[[int], [string]]]::new(); # Read the target file first. Get-Content -Path $TargetFile | ForEach-Object { if (-not [string]::IsNullOrWhiteSpace($_) -and -not $_.Replace(" ", "").StartsWith("#") -and -not $TargetHashes.ContainsKey(($Hash = $_.GetHashCode()))) { $null = $TargetHashes.Add($Hash, $_); } } # Read the source file and prune matching items as we go, while reporting any differences. Get-Content -Path $SourceFile | ForEach-Object { if (-not [string]::IsNullOrWhiteSpace($_) -and -not $_.Replace(" ", "").StartsWith("#")) { if ($TargetHashes.ContainsKey(($Hash = $_.GetHashCode()))) { $null = $TargetHashes.Remove($Hash); } else { [PSCustomObject] @{ File = "Source"; Variation = $_; } } } } # List anything remaining in $TargetHashes as these are implicitly the differences. if ($TargetHashes.Count -gt 0) { foreach ($Entry in $TargetHashes.GetEnumerator()) { [PSCustomObject] @{ File = "Target"; Variation = $Entry.Value; } } } } else { throw "One or both files were not found."; } }With the output looking like:
I simply used directory listing for the two source files but it doesn't matter what format they are - so long as they're text and not binary.
Cheers,
Lain
- VinayNaguruSep 08, 2022Copper ContributorHi Lain. Wow. I will try your script too. Thank you for help me and making my work easier.