Get count of exact word match within a Text file

Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
2022-09-04T09:29:52.293+00:00

237611-image.png

Above is the screenshot of the text file and the requirement here is to get the exact match count for a word "test". So in the above case it should be 2 .

I tried the below code :

(Get-Content "C:\Users\abc\Desktop\POC\Findstring.txt" | Select-String -Pattern "test" -AllMatches).matches.count
But it provides me the value as 9 since it provides a like functionality (it is also considering tester,testing etc in the count)

How should we ensure that we get the count for exact match and not for a LIKE operator scenario (similar to in SQL)

Attaching the sample file237508-findstring.txt

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-09-04T15:01:47.19+00:00

    @SChalakov
    But what if the text was a bit more complicated than that? For example, if the text contains punctuation?

    Also, your regex won't match the last "test" because it's not followed by a space.

    This looks for "word breaks" to delimit the text you're looking for. It also ignores case (which is what the Select-String does). The Get-Content gets the whole file all at once in a single string (by using the "-Raw" switch), including line breaks. The $RegexOptions modifies the regex "." to match every character, including the "\n".

    $Content = Get-Content "C:\junk\Findstring.txt" -raw  
    $RegexOptions = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase,[System.Text.RegularExpressions.RegexOptions]::SingleLine  
    ( [regex]::Matches($Content, "\btest\b", $RegexOptions ) ).count  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-09-04T13:40:35.583+00:00

    Hi,

    this works just fine, just note that the string you are matching against has also whitespaces - one on the left side and one on the right side " test ":

    $Content = Get-Content "C:\Users\Username\Downloads\Findstring.txt"  
    ([regex]::Matches($Content, [regex]::Escape(" test " ))).count  
    

    I hope I could help you out!

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
    Regards
    Stoyan Chalakov

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2022-09-04T15:00:46.66+00:00

    Try using "(test)\b" as the pattern. This code returns 3.

    ("testing Test TEst TESt.foo ____TEST____ testing" | Select-String -Pattern "(test)\b" -AllMatches).Matches.Count  
      
    
     
    
     
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.