将 CSV 文件中的列分组并连接另一列的值,然后导出回 CSV
Group column from CSV file and concatenate values of another column, and export back to CSV
这是对此的跟进:
它与这个问题有关:
这个问题:
https://superuser.com/questions/453041/grouping-labels-and-concatenating-their-text-values-like-a-pivot-table
我的目的是获取我的 csv 文件并按“公司”名称分组,但我不知道从哪里开始,我知道 PS 有一个“Group-Object”CmdLet,但它returns我是一个不知道怎么处理的对象
因此,使用我之前的示例列,来自 Spectre 的 Harvey 将从:
PName
Company
ORDERDATE
STREET1
City
ProjectName
Name
Color
OtherData
Charles
Contoso
2022-01-25
FakeStreet 123
San Francisco
Sacramento Expansion
ProductA
Lemon
OtherData
Harvey
Specter
2022-01-25
NotAFake 123
San Diego
North Dakota Expansion
ProductA
Red
OtherData
Harvey
Specter
2022-01-25
NotAFake 123
San Diego
North Dakota Expansion
ProductB
Blue
OtherData
收件人:
PName
Company
ORDERDATE
STREET1
City
ProjectName
Name
Color
OtherData
Charles
Contoso
2022-01-25
FakeStreet 123
San Francisco
Sacramento Expansion
ProductA
Lemon
OtherData
Harvey
Specter
2022-01-25
NotAFake 123
San Diego
North Dakota Expansion
ProductA, ProductB
Red, Blue
OtherData
感谢并一如既往地感谢
根据你的最后一个问题,假设你已经存储了 $result
,这就是你如何在 [=13= 的帮助下更新你已经拥有的对象]:
$result | Group-Object Company | ForEach-Object {
# If the group is 1 object return it and skip next logic
if($_.Count -eq 1) { return $_.Group }
# Capture all objects of this group
$thisGroup = $_.Group
# We know if we are here that `$thisGroup` is an object[],
# there is at least more than 1 object.
# `$thisObject[0]` is the object we want to update, so,
# `$thisObject[0].PropertyName` will enumerate ALL Values
# of "PropertyName" and we can join them with `-join` and assign
# the result to the 1st Object
$thisGroup[0].Name = $thisGroup.Name -join ', ' # Join these elements
$thisGroup[0].Color = $thisGroup.Color -join ', '
# Return ONLY the first object of this object[], this is the
# object that was updated.
$thisGroup[0]
} | Format-Table -AutoSize
使用相同的 XML 和我之前的答案来构造 $result
,上面代码的输出将是:
PName Company ORDERDATE STREET1 City ProjectName Name Color OtherData
----- ------- --------- ------- ---- ----------- ---- ----- ---------
Charles Contoso 2022-01-25 FakeStreet 123 San Francisco Sacramento Expansion ProductA Lemon OtherData
Harvey Specter 2022-01-25 NotAFake 123 San Diego North Dakota Expansion ProductA, ProductB Red, Blue OtherData
这是对此的跟进:
它与这个问题有关:
这个问题:
https://superuser.com/questions/453041/grouping-labels-and-concatenating-their-text-values-like-a-pivot-table
我的目的是获取我的 csv 文件并按“公司”名称分组,但我不知道从哪里开始,我知道 PS 有一个“Group-Object”CmdLet,但它returns我是一个不知道怎么处理的对象
因此,使用我之前的示例列,来自 Spectre 的 Harvey 将从:
PName | Company | ORDERDATE | STREET1 | City | ProjectName | Name | Color | OtherData |
---|---|---|---|---|---|---|---|---|
Charles | Contoso | 2022-01-25 | FakeStreet 123 | San Francisco | Sacramento Expansion | ProductA | Lemon | OtherData |
Harvey | Specter | 2022-01-25 | NotAFake 123 | San Diego | North Dakota Expansion | ProductA | Red | OtherData |
Harvey | Specter | 2022-01-25 | NotAFake 123 | San Diego | North Dakota Expansion | ProductB | Blue | OtherData |
收件人:
PName | Company | ORDERDATE | STREET1 | City | ProjectName | Name | Color | OtherData |
---|---|---|---|---|---|---|---|---|
Charles | Contoso | 2022-01-25 | FakeStreet 123 | San Francisco | Sacramento Expansion | ProductA | Lemon | OtherData |
Harvey | Specter | 2022-01-25 | NotAFake 123 | San Diego | North Dakota Expansion | ProductA, ProductB | Red, Blue | OtherData |
感谢并一如既往地感谢
根据你的最后一个问题,假设你已经存储了 $result
,这就是你如何在 [=13= 的帮助下更新你已经拥有的对象]:
$result | Group-Object Company | ForEach-Object {
# If the group is 1 object return it and skip next logic
if($_.Count -eq 1) { return $_.Group }
# Capture all objects of this group
$thisGroup = $_.Group
# We know if we are here that `$thisGroup` is an object[],
# there is at least more than 1 object.
# `$thisObject[0]` is the object we want to update, so,
# `$thisObject[0].PropertyName` will enumerate ALL Values
# of "PropertyName" and we can join them with `-join` and assign
# the result to the 1st Object
$thisGroup[0].Name = $thisGroup.Name -join ', ' # Join these elements
$thisGroup[0].Color = $thisGroup.Color -join ', '
# Return ONLY the first object of this object[], this is the
# object that was updated.
$thisGroup[0]
} | Format-Table -AutoSize
使用相同的 XML 和我之前的答案来构造 $result
,上面代码的输出将是:
PName Company ORDERDATE STREET1 City ProjectName Name Color OtherData
----- ------- --------- ------- ---- ----------- ---- ----- ---------
Charles Contoso 2022-01-25 FakeStreet 123 San Francisco Sacramento Expansion ProductA Lemon OtherData
Harvey Specter 2022-01-25 NotAFake 123 San Diego North Dakota Expansion ProductA, ProductB Red, Blue OtherData