Merge 2 CSV by Value “Name” and merge Count (Powershell)

后端 未结 2 1830
礼貌的吻别
礼貌的吻别 2021-01-26 20:12

So at the momemt I\'m searching for a way to merge 2 CSV files.

Here is an example for what I mean:

CSV1

\"Name\",\"Count\"
\"Klaus\",\"3\"
\"Han         


        
2条回答
  •  醉话见心
    2021-01-26 20:59

    You can use Group-Object (alias group) to group everything by the Name property. Then you just need to sum up the Count property of each guy in the group. Measure-Object (alias measure) will do sums for you.

    $grouped = Import-Csv .\csv1.csv, .\csv2.csv | group Name
    $combined = $grouped |%{ 
       New-Object PsObject -Prop @{ Name = $_.Name; Count = ($_.Group | measure -sum -prop Count).Sum }
    }
    $combined | Export-Csv .\combined.csv -NoType
    

提交回复
热议问题