简体   繁体   English

读取.csv并将其导出到全新的csv文件-PowerShell

[英]Read an .csv and export it to a brand new csv file - PowerShell

I'm brand new using powershell, and need some help. 我是使用Powershell的全新用户,需要一些帮助。 I have a csv named test.csv and looks like this: 我有一个名为test.csv的csv,看起来像这样:

----------------------------------
name         lastname       alias        
John         Smith          John              
Marie        Curie          Mary
----------------------------------

The thing is that i need to export the info of the two rows into two different files. 问题是我需要将两行的信息导出到两个不同的文件中。 The name of the file should be the one that is under the 'alias' column 文件名应为“别名”列下的文件名

Example: 例:

Name: John

-----------------------------------
name         lastname       alias        
John         Smith          John

-----------------------------------

Mary

----------------------------------
name         lastname       alias        
Marie        Curie          Mary

----------------------------------

I tried with the following script: 我尝试使用以下脚本:

Import-Csv c:\test\test.csv  | foreach ($line ){New-Item c:\test\$_alias.csv -type file}

Any suggestions? 有什么建议么?

This script ought to work for you. 该脚本应该为您工作。 It creates a new file for each line, using the alias field as the file name. 它使用alias字段作为文件名为每一行创建一个新文件。

$Data = Import-Csv -Path c:\test\test.csv;

foreach ($Item in $Data) {
    Export-Csv -InputObject $Item -NoTypeInformation -Path ('c:\test\{0}.csv' -f $Item.Alias);
}

My solution uses scripting loops and variables: 我的解决方案使用脚本循环和变量:

$path = "c:\test\"
$csv = Import-Csv "$($path)test.csv"
$csv |
%{
    Export-Csv -InputObject $_ -NoTypeInformation -Path "$($path)$($_.Alias).csv"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM