【问题标题】:Split csv file according to matching columns根据匹配列拆分 csv 文件
【发布时间】:2012-01-14 21:00:13
【问题描述】:

我有一个名为 test.csv 的文件,文件中的数据如下所示:

ServerName,Software
"server1.domain.com","Security Update for Windows Server 2003 (KB890046)"
"server1.domain.com","Security Update for Windows Server 2003 (KB893756)"
"server2.domain.com","Microsoft .NET Framework 3.5 SP1"
"server2.domain.com","Microsoft SQL Server 2005"
"server3.domain.com","Security Update for Windows Server 2003 (KB916281)"
"server3.domain.com","Security Update for Windows Server 2003 (KB917159)"

我想将其拆分为每个服务器的文件,使用“ServerName”作为文件名并包含“软件”列中的数据

我希望输出如下所示:

file: server1.domain.com.csv

Security Update for Windows Server 2003 (KB890046)
Security Update for Windows Server 2003 (KB893756)


file: server2.domain.com.csv

Microsoft .NET Framework 3.5 SP1
Microsoft SQL Server 2005"


file: server3.domain.com.csv

Security Update for Windows Server 2003 (KB916281)
Security Update for Windows Server 2003 (KB917159)

我是 Powershell 新手,不知道如何操作。谢谢。

【问题讨论】:

    标签: powershell csv split


    【解决方案1】:

    试试这个,按 ServerName 对内容进行分组。 Foreach 组选择 Software 属性并将其导出到具有每个服务器名称的 csv 文件:

    Import-Csv test.csv | Group-Object ServerName | Foreach-Object{
        $_.Group | Select-Object Software | Export-Csv "$($_.Name).csv" -NoTypeInformation
    }
    

    【讨论】:

    • 谢谢,考虑将其标记为已回答,以防它为您提供所需的内容。
    • 非常感谢谢伊!工作很棒!标记为已回答
    • @user978511,你有相似的用户名:)
    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2017-10-10
    • 2015-03-06
    • 2012-04-14
    • 2020-03-09
    相关资源
    最近更新 更多