Read text file and count matches from another text file

Hi Guys!

Really appreciate your help on this as I already stuck almost a month figuring out the solution. I am creating some tools to count a codes in text files in the folder. The codes will be match with a Master Code list in another text file and the output result will be the number of codes in the files.

Example : code in file A.txt
E15
E15
E15
E22

Master code.txt
E15
E16
E22
E24

The result will be :
E15 : 3
E16 : 0
E22 : 1
E24 : 0

I am able go extract the code in file A.txt. However, I could not find any way to match and count the code list against the Master Code list.

$Codes = Get-content .\CodeList.txt
	for ($j = 0; $j -lt $Codes.Length; $j++)
	{
		$Code = $Codes[$j]
		$Result = ForEach ($File in (Get-ChildItem -Path .\b.txt))
		{
			New-Object PSObject -Property @{
				ReturnCode = $Code
				Count  = @(Get-content $File | Select-String $Code ).count
			}
		}
		$Result | Sort Found -Descending |  Out-File -Append .\counts.txt
		
	}

Thanks in advance for your help.

Please format your code as code using the code tag button (pre). Thanks.

You could start with something like this:

$codeList = Get-Content -Path ‘C:\Sample\code.txt’
$a = Get-Content -Path ‘C:\Sample\a.txt’

foreach ($code in $codeList) {
[PSCustomObject]@{
Pattern = $Code
MatchCount = ($a | Select-String -Pattern $Code).count
}
}

Hi Olaf,

Thanks for your reply, but the output is same as mine. the MatchCount produce 0 output. is there any problem on the .count line?

Thanks,

I used other paths than you. Did you change this accordingly? With the data you provided I get just the expected/desired result.

Pattern MatchCount
------- ----------
E15              3
E16              0
E22              1
E24              0

Hi Olaf,

Thank you, it was due to the codes in Codelist.txt file contains spaces after which probably detected as not match. I get the desired result after removing the spaces. Thanks a lot! another thing Olaf, is there any way the code not in the Codelist.txt to be displayed on the same result?

Thanks

OK. To ignore the additional space charachters you could use the .trim() method …

$codeList = Get-Content -Path 'C:\Sample\code.txt'
$a = Get-Content -Path 'C:\Sample\a.txt'

foreach ($code in $codeList.trim()) {
    [PSCustomObject]@{
        Pattern    = $Code
        MatchCount = ($a | Select-String  -Pattern $Code).count
    }
}

If I understood you right you’d need another logic to get what you need. If you just like to get the results displayed on the console you could count on the output optimization of the Powershell console and add the following code after the first snippet:

Compare-Object -ReferenceObject $codeList.trim() -DifferenceObject ($a | Select-Object -Unique ) -IncludeEqual |
    Where-Object {$_.SideIndicator -eq '=>'} |
        Select-Object -Property @{Name = 'Pattern'; Expression={$_.InputObject}},@{Name = 'MatchCount'; Expression={0}}

Hi Olaf,

Yes u are right, I just realized… Should the code did not exist in the CodeList.txt, it need to be count as well. Thanks for the code, but I am not sure where can i add the code as for the current result i already add it in a variable as follows :

	$Result = foreach ($Code in $codeList)
	{
		[PSCustomObject]@{
			Pattern	     = $Code
			MatchCount   = ($a | Select-String -Pattern $Code).count
		}
		
	} 
	
	$Result | Out-File -Append .\counts.txt

Because I don’t know what you like to do with the results I don’t know what to recommend for you. If you like it just outputted to console you could simply run both snippets after each other:

$codeList = Get-Content -Path 'C:\Sample\code.txt'
$a = Get-Content -Path 'C:\Sample\a.txt'

foreach ($code in $codeList.trim()) {
    [PSCustomObject]@{
        Pattern    = $Code
        MatchCount = ($a | Select-String  -Pattern $Code).count
    }
}
Compare-Object -ReferenceObject $codeList.trim() -DifferenceObject ($a | Select-Object -Unique ) -IncludeEqual |
    Where-Object {$_.SideIndicator -eq '=>'} |
        Select-Object -Property @{Name = 'Pattern'; Expression={$_.InputObject}},@{Name = 'MatchCount'; Expression={0}}

If you like to have it in a combined CSV file you could use add an

Export-Csv -Path 'C:\sample\results.csv' -NoTypeInformation -Append

Of course you should add this export to both snippets. :wink:

Hi Olaf,

Thanks, my objective is to count the code in a.txt file with the CodeList.txt and produce the result in results.txt file. If the code in a.txt file did not exist in CodeList.txt, the program will still count it and produce the output result.txt… is it possible?

Thank you,

Of course you can export the results of both snippets to a single text file. But don’t you think a CSV file is more suitable? Both options would need a parameter -Append to the cmdlet you use … either Out-File or Export-Csv .

Hi Olaf,

Thanks, I managed to get both output in the .csv file but the count on the 2nd snippet return 0. Is it because of Expression = {0}?

Thank You,

Yes. Because it does not have an according code in the code list. :wink: If you just want to count the occurences of the codes in one file you can get this easier.

$a | Group-Object | Select-Object -Property Name,Count