根据匹配的列拆分csv文件 [英] Split csv file according to matching columns

查看:151
本文介绍了根据匹配的列拆分csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a file called test.csv, Data in the file is looking like this:

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作为文件名并包含软件列中的数据将每个服务器的文件拆分成

I want to split it into files for each server using "ServerName" as file name and containing the data from the "Software" column

I希望输出看起来像这样:

I would like the output to look like this:

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和无法解决如何做到。感谢。

I'm new to Powershell and can't work out how to do it. Thanks.

推荐答案

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

Try this, group the content by ServerName. Foreach group select the Software property and export it to a csv file with a name of each server:

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

这篇关于根据匹配的列拆分csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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