csv - 对 CSV 文件中的列进行分组并连接另一列的值

标签 csv powershell

我刚刚开始使用 PowerShell,在任何地方都找不到这个问题的答案。
尝试编写一个脚本来导入 CSV 文件并搜索该文件以查看是否有任何名称相同。如果名称相同,我想将所有这些值写在同一行上,并且只显示名称一次。

例如:

Name   Number    
Tom    1  
Tom    4  
Bill   3  
Dave   2  
Dave   5   
Dave   6  

有了这个输出:

Tom- The number(s) associated with this person is/are: 1/4  
Bill- The number(s) associated with this person is/are: 3  
Dave- The number(s) associated with this person is/are: 2/5/6

最佳答案

像这样的管道应该可以完成这项工作

Import-Csv .\test.csv |   
  Group-Object Name |  
  Foreach-Object { 
    $nums = $_.Group.Number;
    "{0}- The number(s) associated with this person is/are: {1}" -f $_.Name, ($nums -join '/') 
  }

关于csv - 对 CSV 文件中的列进行分组并连接另一列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38113586/

相关文章:

python - 读取多个 CSV 文件并水平连接它们

javascript - 无法写入 CSV 文件

powershell - 在 Powershell 中使用调用异步复制项目

powershell - 在文本文件中交换 last.first 到 first.last

python - 在 Python 中将空格写入 CSV 字段?

python - Scrapy python csv 输出在每行之间有空行

c# - 从 csv 文件读取并在 .net 中处理它

powershell - Azure 管道 - 将 .coverage 转换为 xml 以生成代码覆盖率报告

powershell - 无法在 PowerShell 中将 curl 转换为 Invoke-WebRequest(--不安全/-k 未找到)

powershell - 将文件夹中的每个 .flac 文件转换为 320kbps .mp3,而不丢失元数据