Combine two or more rows in a csv using powershell

后端 未结 1 1912
旧巷少年郎
旧巷少年郎 2020-12-22 09:52

I receive a file from one of our clients that looks like this: (I added headers to this to it for easier processing). Sample Input:

PlanID,Date,Transaction,T         


        
相关标签:
1条回答
  • 2020-12-22 10:14
    Import-Csv input.csv | Group-Object planid, transaction | 
    Select @{n="PlanId";E={($_.name -split ',')[0]}},
           @{n="Date";e={($_.group)[0].date}}, 
           @{n="Transaction";E={(($_.name -split ',')[1]).trim()}},
           @{n="Type";e={($_.group)[0].type}},  
           @{n="Ticker";e={($_.group)[0].ticker}}, 
           @{n="Amount";e={($_.group | measure-object amount -sum).sum}},
           @{n="Cash";e={($_.group)[0].cash}} |
    Export-Csv output.csv -NoTypeInformation
    

    Steps:

    1. Import the input.csv file
    2. Group the objects by planid and transaction
    3. Select the properties you want in your output
      1. PlanId = whatever is left of the comma in the name field of the object returned by group-object
      2. Transaction = whatever is right of the comma in the name field of the object returned by group-object
      3. Amount = sum of all amounts per grouped object
      4. Date = the date per grouped object
      5. Type = type per grouped object
      6. Ticker = ticker per grouped object
      7. Cash = cash per grouped object
    4. Export the objects to output.csv (add notypeinformation to strip the top line with type info)
    0 讨论(0)
提交回复
热议问题