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 回答