Search code examples
powershellcompareline-numbers

Compare Text Files. Record Line #s that Match


I have two text files.

$File1 = "C:\Content1.txt"
$File2 = "C:\Content2.txt"

I'd like to compare these to see if they have the same number of lines and then I'd like to record the line number of each line that matches. I realize that sounds ridiculous but this is what I've been asked to do at my work.

I can compare them a lot of ways. I decided to do the following:

$File1Lines = Get-Content $File1 | Measure-Object -Line
$File2Lines = Get-Content $File2 | Measure-Object -Line

I'd like to test it with an if statement so that if they don't match, then I can start an earlier process over again.

if ($file1lines.lines -eq $file2lines.lines) 
{ Get the Line #s that match and proceed to the next step} 
else {Start Over}

I'm unsure how to record the line #s that match. Any thoughts on how to do this?


Solution

  • This is really pretty simple since Get-Content reads the file in as an array of strings, and you can index that array simply enough.

    Do{
        <stuff to generate files>
    }While(($File1 = GC $PathToFile1).Count -ne ($File2 = GC $PathToFile2).count)
    
    $MatchingLineNumbers = 0..($File1.count -1) | Where{$File1[$_] -eq $File2[$_]}
    

    Since arrays in PowerShell use a 0 based index we want to start at 0 and go for however many lines the files have. Since .count starts at 1 not 0 we need to subtract 1 from the total count. So if your file has 27 lines $File1.count will equal 27. The index for those lines ranges from 0 (first line) to 26 (last line). The code ($File1.count - 1) would effectively come out to 26, so 0..26 starts at 0, and counts to 26.

    Then each number goes to a Where statement that checks that specific line in each file to see if they are equal. If they are then it passes the number along, and that gets collected in $MatchingLineNumbers. If the lines don't match the number isn't passed along.