Search text file for 2 items of text at specific positions and return the count

Hi.
I am working with some very large flat text files and I need to be able to search the file for 2 bits of text at different positions in the line of data then return how many lines contain these 2 bits of text.
So for example I want to know how many lines in the file has the word “bob” where “b” is position 1, “o” is position 2 and “b” is position 3. then in the same line I need to also find “sam” at position 30,31 & 32. I only want to count the row if both elements are present and in the correct positions.

Thanks

I would attempt to use the Select-String and Group-Object cmdlets to solve this.

Select-String online help
https://technet.microsoft.com/en-us/library/Hh849903.aspx

Group-Object online help
https://technet.microsoft.com/en-us/library/hh849907.aspx

It’s pretty easy to do with RegEx.

$pattern = [regex]"^bob.{26}sam"
$count = 0
Get-Content -Path .\data.txt | foreach {
    if ($_ -match $pattern) { $count++ }
}
"Found $count hits"

The dot after bob is a wildcard that matches any character, and we will match exactly 26, which pushes sam out to column 30 (1 + 3 + 26). Obviously the more precise you can get about the match, the better. For instance, not knowing what else might be on the line, this particular regex will match sam or samuel as long as it starts in column 30.

That worked famously!
Thanks!!