0

私はこのpsコードを持っています

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)

..

ps スクリプトを拡張して 0+1+2=3 の合計を報告する方法

4

2 に答える 2

5

次のように正規表現で数値をキャプチャします。

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 :

参考までに、メンバーの列挙をサポートする PowerShell V3 でこれをテストしました。V2 では、これを行う必要がある場合があります。

PS> "0 warnings","1 warnings","5 warnings" | Select-String "(\d+) warnings" | 
        Foreach {$_.Matches | Foreach {$_.Groups[1].Value}} | Measure -Sum
于 2012-10-01T21:33:32.313 に答える
2

これをもっと速く試すことができます:

  PS II>  $s="0 warnings","1 warnings","5 warnings" 
  PS II>  [regex]::matches($s,"(\d+)\s*warnings") | measure -inp {$_.Groups[1].Value} -sum
于 2012-10-02T15:19:16.607 に答える