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.
$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.
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 :
[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???