【发布时间】:2021-08-06 19:34:56
【问题描述】:
我正在使用脚本从 CSV 文件中获取一些数据,但不知道解决最重要的部分 - 我有一个包含几百行的数组,这些行中有大约 50 个 Id,每个 Id 都有附加了一些不同的服务。每条线都附有价格。
我想按 ID 和服务对行进行分组,并且我希望将这些组中的每一个都放在某种变量中,这样我就可以对价格求和。我之前在脚本中过滤掉了唯一的 ID 和服务,因为它们总是不同的。
一些示例数据:
$data = @(
[pscustomobject]@{Id='1';Service='Service1';Propertyx=1;Price='5'}
[pscustomobject]@{Id='1';Service='Service2';Propertyx=1;Price='4'}
[pscustomobject]@{Id='2';Service='Service1';Propertyx=1;Price='17'}
[pscustomobject]@{Id='3';Service='Service1';Propertyx=1;Price='3'}
[pscustomobject]@{Id='2';Service='Service2';Propertyx=1;Price='11'}
[pscustomobject]@{Id='4';Service='Service1';Propertyx=1;Price='7'}
[pscustomobject]@{Id='2';Service='Service3';Propertyx=1;Price='5'}
[pscustomobject]@{Id='3';Service='Service2';Propertyx=1;Price='4'}
[pscustomobject]@{Id='4';Service='Service2';Propertyx=1;Price='12'}
[pscustomobject]@{Id='1';Service='Service3';Propertyx=1;Price='8'})
$ident = $data.Id | select -unique | sort
$Serv = $data.Service | select -unique | sort
我们将不胜感激!
【问题讨论】:
标签: powershell