Powershell Group By Multiple Properties

后端 未结 1 1011
予麋鹿
予麋鹿 2020-12-19 05:15

I am trying to determine if there\'s an easier way to write a Powershell function that groups an array by multiple properties and sums specified properties in the group, sim

相关标签:
1条回答
  • 2020-12-19 05:55

    I have the same, perhaps a bit simpler, I remove two |

    $grouped = $ungrouped | group ID, ID2
    $grouped | foreach {
        $b= $_.name -split ', '
        [pscustomobject] @{
             ID = $b[0];ID2 = $b[1]
            'Sum Value' = ($_.group | measure value -sum).sum
        }
    }
    

    One liner :

    Import-Csv 'YourFile.csv' | Group-Object -Property ID,ID2 | % {$b=$_.name -split ', ';$c=($_.group | Measure-Object -Property value -Sum).Sum;[PScustomobject]@{ID=$b[0];ID2=$b[1];Sum=$c}}
    
    0 讨论(0)
提交回复
热议问题