I have a script that counts how many times a certain word occurs within a file. With the file it keeps adding words and each time the powershell is suppose to count the occurance. However I want to it stop counting once its identified a specific word has been added three times eg:
apple apple apple banana banana apple
I want it to stop counting once its identified apple 3 times and not count it as 4. this is the powershell script that i'm using :
$ht=@{}
$Output=switch(gc c:\temp\LimitCount.txt){ {($ht.values|sort -Descending|select -first 1) -eq 3}{break}
{$_ -match '^. +user:(.+)$' -and [string]::IsNullOrEmpty($ht.(([regex]"^(?:. +user:)(.+)$").matches($_).groups[1].value))}{$ht.(([regex]"^(?:. +user:)(.+)$").matches($_).groups [1].value)=0}
{$_ -match '^. +user:(.+)$' -and $ht.(([regex]"^(?:. +user:)(.+)$").matches($_).groups[1].value) -eq 3}{continue}
{$_ -match '^. +user:(.+)$'}{$ht.(([regex]"^(?:. +user:)(.+)$").matches($_).groups[1].value)++;([regex]"^(?:. +user:)(.+)$").matches($_).groups[1].value;continue}
default {$_}
}
"Unfiltered results and count:"
$output|group -NoElement
$output|Where{$ht.keys -contains $_}|group -NoElement