by Remigiusz at 2013-03-08 12:04:46
Hiby DexterPOSH at 2013-03-08 21:48:36
I have object Report like this below
Name value count
-------------------------------------------
Alan low 8
John high 3
Alan low 5
Andrew low 2
Alan high 1
Andrew high 2
could you help me to group items in this object to get group for each change in Name and each change in value, count should be summed so I should get something like below
Alan low 13
John high 3
Andrew low 2
Alan high 1
Andrew high 2
For every change in column Name check column value and when you find change group Name, value and make sum of column count where ex. Name is Alan and value low, next Name Alan value high etc.
How can I do this ?
Hi Remigiusz,by mjolinor at 2013-03-10 09:24:39
Can you explain a bit more. What you want to achieve?
One way is to accumulate the counts using a hash table with the Name and Value as the keys, and then create a new set of objects from the hash table:by mikefrobbins at 2013-03-10 12:45:28$ht = @{}
$objects |
foreach {$ht["$($.Name) $($.Value)"] += [int]$.count}
$ht.keys |
sort |
foreach {
New-Object psobject -Property @{
Name = $.split()[0]
Value = $.split()[1]
Count = $ht.$
}
} |
select Name,Value,Count|
Format-Table -AutoSize
Name Value Count
---- ----- -----
Alan high 1
Alan low 13
Andrew high 2
Andrew low 2
John high 3
I copy and pasted your data into notepad and saved it as a csv (meaning it uses a space as a delimiter).Import-Csv U:\tmp\data.csv -Delimiter ' ' |
Group-Object -Property name, value |
foreach {"$($.Name), $(($.Group | measure-object -property count -sum).sum)"}
The output of the above one-liner looks like this:
Alan, low, 13
John, high, 3
Andrew, low, 2
Alan, high, 1
Andrew, high, 2