我有以下powershell脚本。
$groups = gc $fn |
Select -Property @{name='G1'; expression={$_.SubString(340, 7)}},
@{name='G2'; expression={$_.SubString(32, 2)}},
@{name='V1'; expression={$_.SubString(420, 8)}},
@{name='V2'; expression={$_.SubString(43, 11)}} |
group G1,G2
$groups | % {
$g = $_.Group | ? { [float]($_.V1) -ne 0 } | measure V1 -Sum #V2???
$_.Name, $g
}
但是我希望结果如下。如何包含V2的总和?如何为最终结果生成对象列表?
Name SumOfV1 SumOfV2 G1, G2 3243243 2432432 .....
可行的例子:
ps | select -Property @{name='g1'; expression = {$_.Name}},
#@{name='g2'; expression = {$_.Id}},
@{name='v1'; expression = {$_.PM}},
@{name='v2'; expression = {$_.WS}} |
group g1 | % {
[pscustomobject] @{
Name = $_.Name
Sum = $_.Group | Measure V1,V2 -Sum | select -ExpandProperty Sum
}
}
答案 0 :(得分:1)
小蛋糕,甚至不需要看到你的数据是诚实的。在ForEach循环中,只需创建一个PSCustomObject以输出具有3个属性的管道,组的名称,V1的总和以及V2的总和。它可以这样做:
$groups = gc $fn |
Select -Property @{name='G1'; expression={$_.SubString(340, 7)}},
@{name='G2'; expression={$_.SubString(32, 2)}},
@{name='V1'; expression={$_.SubString(420, 8)}},
@{name='V2'; expression={$_.SubString(43, 11)}} |
group G1,G2
$groups|ForEach{
[pscustomobject]@{
'Name'=$_.name
'V1Sum'=$_.group|measure V1 -sum|select -expand sum
'V2Sum'=$_.group|measure V2 -sum|select -expand sum
}
}
我制作了测试数据(yay get-random!):
G1 G2 V1 V2
-- -- -- --
Happy Meals 97 20
Happy Meals 71 21
Happy Meals 24 54
Tickle Fight 87 19
Tickle Fight 14 18
Tickle Fight 25 0
Tickle Fight 78 51
这提供了输出:
Name V1Sum V2Sum
---- ----- -----
Happy, Meals 192 95
Tickle, Fight 204 88