Merging informations in a csv file .

Hi folks, i have a csv file and i want to merge the informations in it .

For example the input is like

Name ,   Number, slot
A         , 5               , 7
B         , 1               , 10
C         , 3               , 6
B         , 10            , 2
A         , 9              , 2

And i want to sum up Nb and slot for the same users have

Name, Nb, Slot
A, 6, 6
B, 13, 7
C, 4, 10
D, 4, 13

This is just an example, the file i'm using is with reel usernames and around 1000 rows .

Any help will be grateful , i'm quite new in powershell .

May 28th, 2015 7:02am

Sorry the output is not correct , the correct one is

Name, Nb, Slot

A, 14, 9
B, 11, 12
C, 3, 6

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 7:06am

$CSV = "Name,Number,slot
A,5,7
B,1,10
C,3,6
B,10,2
A,9,2"

$Data = ConvertFrom-Csv $CSV

$Result = ForEach ($User in ($Data | Group Name)) {
    [PSCustomObject]@{
        Name = $User.Name
        Nb = ($User.Group | Measure-Object Number -Sum).Sum
        Slot = ($User.Group | Measure-Object Slot -Sum).Sum
    }
}

$Result | ConvertTo-Csv -NoTypeInformation

#Output
"Name","Nb","Slot"
"A","14","9"
"B","11","12"
"C","3","6"

May 28th, 2015 7:20am

This is the easy way:

Import-Csv file.csv |
    group -property name |
    select name,
        @{N='Nb';E={$_.Group | (Measure -Sum Number).Sum}},
      @{#add sum for slot} 



Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 9:57am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics