I’m in a tricky situation to analyze a text output. My requirement is search a string and get a lines between them. For example, the following lines in a text file
What have you tried so far? Please show your code and explain what is not working as expected or where you’ve got stuck. Include error messages if there are some.
I’ve got some knowledge in Powershell. I actually tried various method but something with regex gives little bit But it can give result for two matches matches and not sure a loop possible here, but I think we can achieve through StreamReader readline and filter by particular string?, I dont know how to do and not much help from internet
$File = Get-Content .\log.txt
$pat = "(?:[\w]\:)(\\[a-z_A-Z\-\s0-9\.]+)+\.log"
$repeat = Select-String .\logger.txt -pattern "(?:[\w]\:)(\\[a-z_A-Z\-\s0-9\.]+)+\.log"
for ($i=0; $i -lt $repeat.Count; $i++) {
$patt = "$pat(.*?)$pat"
$d = [regex]::Match($File,$patt).groups[2].value
$f = Select-String -InputObject $d -pattern "sc-status=200"
Write-host "The Number of Errors in the File $($e.matches[$i]) : $(($f -split('Sc-status=200')).Count-1)"
}
I hoped that would help me understand what you’re actually trying to do but it didn’t actually.
Could you please describe in simple terms what you actually want to do?
I understood that you try to count the occurences of a particular pattern in some files. Is that right?
Yes, need a occurrences of particular pattern but all the content in one file. so I need explore a file to pull the details.
Actually I’ve a output of generated by a system which has multiple log reading with specific codes having a log file name at the top. I need to find out - How many code for specific files which is in the output file? so I’m going through no of lines under each header (filename) and collect it
If it is what I assume it is and I assume we have file names in the format you posted in your initial post and you’re looking for patterns equal to those in your initial post then something like this should be enough:
I did not get until now that you’re looking for this information, sorry, I’m not a native English speaker. That implies that you always have a maximum of 2 occurences of the pattern per file, right?
If only I would understand what you mean. The groups represent the different files in this case. You know that, don’t you? I thought you need the lines between the occurences of the pattern per file!?
All the text is in 1 file only, example log.txt contains the following text
Pattern 1
Data1
Data2
Data3
Pattern 2
Data4
Data5
Data6
Data7
Pattern 3
Data8
Data9
Data10
it goes like several lines
output I expect
Pattern 1 has 3 lines
Pattern 2 has 4 lines
Pattern 3 has 3 lines
Your grouping concept is correct. Actually Pattern = somefilename.log not a file itself
Thank you so much Olaf. My last post code worked now with some conditional loops. It’s a multiple occurrence of patterns and need to find out the lines underneath them
Great. I’m glad it helped. You may post the final version of the code or you could correct / complete the last code you posted. That could help others in the future looking for the same or a similar issue.
Please check below my Final version. Once again thanks for your assistance
$filePath = Get-Content .\logger.txt
$filePattern = Select-String -Path .\logger.txt '(?:[\w]\:)(\\[a-z_A-Z\-\s0-9\.]+)+\.log' -AllMatches |
Group-Object -Property Path
# First we need to check for last Pattern which doesn't have next pattern
$lineCount = if($filePattern) {
[PSCustomObject]@{
"File Name" = $filePattern.group[-1].line
"No of Errors" = ($filePath.Length) - $filePattern.group[-1].LineNumber
} # This gives output for last pattern, otherwise we get a weird number as there may be loop issue
}
# For the rest of Pattern First to Last-1
for ($i=0; $i -lt $filePattern.Count-1; $i++) {
[PSCustomObject]@{
"File Name" = $filePattern.group[$i].line
"No of Errors" = $filePattern.group[$i+1].LineNumber - $filePattern.group[$i].LineNumber -1
}
}
$lineCount
I’ve been following along today and wishing I was at my computer. This was fun, here’s what I came up with.
My thought process included what if there are extra blank lines between the matches that shouldn’t be counted as an error. With that in mind, I updated the original sample data some.