Multi-line pattern looking for contained content in file

Good day to all,

Okay i am not new to PS but still consider myself green. I have a pattern file with multiple lines of content( 3-4 lines) and I want to use this as a pattern to compare against another file and count the number of occurrences that the file matches the pattern file 3-4 lines per occurrence.

Caveat on this is the text being searched for is not at the start of the line, each line has a date,timestamp,etc. I would very much appreciate any assistance I can get on this.

Have you started with any code, If yes it’ll help us in better understanding where you are stuck, else You can have a look at help documentation of Select-String cmdlet.

Here’s a basic multi line search example.

PS C:\users\js> get-content -raw text | select-string joe\r\njohn

joe
john
james
$reg4file = "C:\Install\logbackup\opos\REG4_oposcashdrawer.log"
$reg4_poscount = (get-content $reg4file | Select-String -pattern "> Rx pop drawer 1 from POS").length
$reg4_poscount
$reg4_totalcount = (get-content $reg4file | Select-String -pattern "> Cash Drawer 1 Opened. evt").length
$reg4_totalcount

This is what i have the files i am running contain the pattern noted but when i call the variable i always get “0”(zero)

for both results.

Unfortunately this is all i can provide as it the rest is sensitive in nature, but if clarification is needed please ask.

This is another way.

$TextData = @'
Joe    ........ John
  ---- James
Debra
'@

[RegEx]::Matches((Get-Content -Path 'Variable:\TextData'),'Joe|John').Value

# Results

Joe
John

As for this…
Okay i am not new to PS but still consider myself green.

… then really, spend the time getting ramped up to limit / avoid, the most common misconceptions, confusion, frustration, bad habits, errors you are going to encounter.

If you are a visual learner, live on these sites, and search for ‘Beginning PowerShell’, ‘Intermediate PowerShell’, etc… Or just ‘PowerShell’. Leverage all the no cost resources all over the web.

YouTube, MSDN Channel9, MS Learn. Microsoft Virtual Academy, TechNet Virtual Labs

Find any lab, on TechNet, that has PowerShell in the mix. I.e., Exchange, Lync/Skype, Azure, O365, etc…

Windows PowerShell Survival Guide
https://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

See threads like this one…
https://www.reddit.com/r/PowerShell/comments/ar6cvt/powershell_in_depth_second_edition/egmlpom/?context=3

I understand that script fairly well i think, however i am trying to count the number of times that a string pattern occurs in the file not pull the value of the line or pattern.

# your line endings may vary, osx is just \n
$regex = [regex]'joe\r\njohn'

# maybe this way, this is quite a rabbit hole
# $regex = [regex]'(?s)joe.{1,2}john'

PS /Users/js> $file = get-content -raw text                                  
PS /Users/js> $regex.matches($file)                                          


Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 8
Value    : joe
           john

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 9
Length   : 8
Value    : joe
           john

PS /Users/js> $regex.matches($file) | measure                                


Count             : 2
Average           : 
Sum               : 
Maximum           : 
Minimum           : 
StandardDeviation : 
Property          : 

You can use a variable like a file? That’s good. I can use that.

$reg1file = "C:\Install\logbackup\opos\REG1_oposcashdrawer.log"
$reg1_poscount = (get-content $reg1file | Select-String -pattern "> Rx pop drawer 1 from POS").length
$reg1_poscount
$reg1_totalcount = (get-content $reg1file | Select-String -pattern "> Cash Drawer 1 Opened. evt").length
$reg1_totalcount

Output:

PS C:\Windows\system32> $reg1file = "C:\Install\logbackup\opos\REG1_oposcashdrawer.log"
$reg1_poscount = (get-content $reg1file | Select-String -pattern "Rx pop drawer 1 from POS").length
$reg1_poscount
$reg1_totalcount = (get-content $reg1file | Select-String -pattern "Cash Drawer 1 Opened. evt").length
$reg1_totalcount
0
0

I am 100% that the file contains both line at one ore more lines in the file yet i am still returning zeros.

This is what the file lines look like

03/28/2019 09:00:40.330 [t5052] OposCashDrawer: <100004:Trace> Rx pop drawer 1 from POS
03/28/2019 09:00:40.331 [t5052] OposCashDrawer: <100004:Trace> Cash Drawer 1 Opened. evt=[0] State=[1]

anyone have any ideas…still stuck on this and not making much sense now

I get 1’s when I run your example.

could it be something to do with the formatting of the log file itself?

i have not been able to get it to return 1 even when i copy and pasted the exact chars+ws from the file

Is the forum reflecting the contents of your file properly? It can’t display what look like xml tags. This works for me:

PS C:\> ('03/28/2019 09:00:40.330 [t5052] OposCashDrawer: <100004:Trace> Rx pop drawer 1 from POS' | Select-String -pattern "Rx pop drawer 1 from POS").length
1

$reg4file = “C:\Install\logbackup\opos\REG4_oposcashdrawer.log”
$reg4_poscount = (get-content $reg4file | Select-String -pattern ‘Rx pop drawer 1 from POS’).length
$reg4_poscount.count
$reg4_totalcount = (get-content $reg4file | Select-String -pattern ‘Cash Drawer 1 Opened. evt’).length
$reg4_totalcount.count

 

Okay so with this code i am returning 1 but in the file there are more than 50 lines that contain the string value.

^^ Props to Js for kicking my brain

[quote quote=147530]could it be something to do with the formatting of the log file itself?

i have not been able to get it to return 1 even when i copy and pasted the exact chars+ws from the file

[/quote]

it was something in the formatting i tested one of the files and simply did a (Copy ALL, PASTE) no changes/modifications to any line and saved after that i am getting the actual count of lines containing the string…now how do i accomplish this through powershell???

Maybe it’s the encoding? If you open one in notepad and do save as, what is the encoding, unicode?

the ones i completed the COPY/PASTE on are running as unicode however the file i am having the issues with is set for ANSI…how do i modify this ??

i have now figured that part out as well using system.io.file

[system.io.file]::readalltext($file) | out-file $file -encoding unicode

 

 

I appreciate everyones assistance on this. i will follow up if there is something else that i hit a snag on

Weird. Maybe it was utf8 no bom or something.

Okay so it does not appear that the encoding is the issue.

When i copy and paste…same document no other change/mods…then save it will parse correctly and give the proper number of occurences…

I have tried changing the encoding with no success, ONLY if i have copy/pasted back into the file does it read correctly