0

I need to get the occurrence of "failure" in a logfile. The thing is I need to get the occurrence of "failure" for each session block. What the log looks like:

---Session 1 
check 
check 
failure
failure 
check
----Session 2 
check 
failure 
check 
check 
----Session 3 
failure 
failure

What I've got so far is this:

$rows = Get-Childitem -Path E:\shell\lot.log  |
        Select-String -Pattern failure
$i = 0
foreach ($row in $rows) {
    $i++
}
echo $i

With that script I only get the total of the occurrences.

2
  • So there's only 1 log file, with multiple Session blocks? If so, why the use of -Recurse? Commented May 23, 2018 at 21:20
  • @mklement0 oh sorry i didnt delete it.I've edited it. Yes it has been multiple logs. For each session one. But now there is only one file. Commented May 23, 2018 at 21:24

3 Answers 3

1

I would start a new counter whenever a line beginning with 3 or more consecutive hyphens occurs and collect the results in a hashtable.

$failcount = @{}

Get-Content 'E:\shell\lot.log' | ForEach-Object {
    if ($_ -match '^-{3,}(.*)') {
        $section = $matches[1].Trim()
        $failcount[$section] = 0
    } elseif ($_ -like '*failure*') {
        $failcount[$section]++
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I believe this will do it. Key part is to reset your counter after each session.

$rows = Get-Childitem -Path E:\shell\lot.log

$i = 0  # failure counter
$j = 1  # session counter

foreach($row in $rows){
    if($row -like "*Session 1"){
        # skip the first line. Edit: removed * as would skip session 10, 11 etc. assumes line ends with "session 1"
        continue
    }elseif($row -eq "failure){
        # if the row is equal to failure, count it
        $i++
    }elseif($row -like "*Session*"){
        # when you get to the end of a session, print the number of failures
        Write-Host "Session $j had $i failures"

        # ... and reset the failure counter 
        $i = 0

        # increment the session counter
        $j++
    }
}

4 Comments

looks great. But when i execute it, there is no occurrence echo
Does your file have lines like ----Session 2 in it? echo is replaced by the native Write-Host.
I know what write-host means :D yes it has, why?
It's just you wrote echo, I wasn't sure ;-). Hmm not sure why that elseif isn't being ran then...
0

I'll add another option. Read the whole log file in with -Raw to get a multi-line string. Remove the ---- from the first line, and then split on 3 or more hyphens at the beginning of a line, this gets you each session as a multi-line string, then you could just output text or custom objects, or whatever you wanted with it. Split the multi-line string on new line characters, filter for 'failure', and do a count to get failures per session.

(GC E:\shell\lot.log -Raw) -replace '^-+' -split '(?<=[\r\n])---+'|%{
    '{0} had {1} failure(s)' -f ($_.split("`n")[0].Trim()),($_ -split '[\r\n]+'|?{$_ -match 'failure'}).Count
}

That would (given the sample provided) output:

Session 1 had 2 failure(s)
Session 2 had 1 failure(s)
Session 3 had 2 failure(s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.