0

I have this ps code

PS > Select-String -path .\build-count-warn.txt -pattern "[1-9]?[0-9]+ warn"

warn.txt:1:    0 Warning(s)
warn.txt:2:    1 Warning(s)
warn.txt:3:    2 Warning(s)

..

So how to extend the ps script and report the sum of 0+1+2=3

2 Answers 2

5

Capture the numeric value in the regex like so:

PS> "0 warnings","1 warnings","5 warnings" | Select-String "(\d+) warnings" | 
        Foreach {$_.Matches.Groups[1].Value} | Measure -Sum


Count    : 3
Average  :
Sum      : 6
Maximum  :
Minimum  :
Property :

FYI I tested this on PowerShell V3 which supports member enumeration. On V2, you may need to do this:

PS> "0 warnings","1 warnings","5 warnings" | Select-String "(\d+) warnings" | 
        Foreach {$_.Matches | Foreach {$_.Groups[1].Value}} | Measure -Sum
Sign up to request clarification or add additional context in comments.

Comments

2

you can try this, much faster:

  PS II>  $s="0 warnings","1 warnings","5 warnings" 
  PS II>  [regex]::matches($s,"(\d+)\s*warnings") | measure -inp {$_.Groups[1].Value} -sum

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.