CSV中的Powershell组对象并导出 [英] Powershell Group Object in CSV and exporting it

查看:98
本文介绍了CSV中的Powershell组对象并导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#foo.csv
No, Fruit, Quality, Units, Location
1, Orange, New, 10, Village
2, Orange, Fresh, 20, City
3, Apple, New, 15, Village
4, Grapes, Fresh, 25, City
5, Apple, Fresh, 30, City
6, Apple, Fresh, 35, City

使用此数据,我想以以下方式输出:
水果位置|品质|单位

With this data I want output in following way:
Fruit | Location | Quality | Units

我无法进一步对(子组?)数据进行分组,下面是我附带的内容,但不是我所需要的:

I am not able to further group (sub-group?) data, and what I came with is below, but its not what I need:

Import-Csv "E:\foo.csv"  | 
Group-Object { $_.Fruit } | 
Select-Object -Property Count, 
    @{ Name = 'Fruits'; Expression = { $_.Name } }, 
    @{ Name = 'Location'; Expression = { ($_.Group | Measure-Object -Property Units -Sum).Sum } } | 
Export-Csv new.csv  -NoTypeInformation

我进一步尝试:

Import-Csv "E:\foo.csv"  | 
Group-Object { $_.Fruit } | 
Select-Object -Property Count, 
    @{ Name = 'Fruits'; Expression = { $_.Name } }, 
    @{ Name = 'Location'; Expression = { $_.Location } },
    @{ Name = 'Quality'; Expression = { $_.Quality } },
    @{ Name = 'Units'; Expression = { ($_.Group | Measure-Object -Property Units -Sum).Sum } } | Sort-Object -Property Count | Format-Table -Auto

但是数据为空.

推荐答案

您可以同时按多个属性进行分组:

You can group by multiple properties in the same time:

Import-Csv "E:\foo.csv"|
Group-Object Fruit,Location,Quality|
Select-Object @{Name='Fruit'   ;Expression={$_.Values[0]}},
              @{Name='Location';Expression={$_.Values[1]}},
              @{Name='Quality' ;Expression={$_.Values[2]}},
              @{Name='Units'   ;Expression={
                  ($_.Group|Measure-Object Units -Sum).Sum
              }}

这篇关于CSV中的Powershell组对象并导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆