简体   繁体   English

如何使用 Powershell 合并多个 CSV 文件,其中两列具有相同的数据

[英]How can i merge multiple CSV files in which two columns have same data using Powershell

Am very new to powershell topic, I just want a help from you all.我对 powershell 主题非常陌生,我只是想得到大家的帮助。 I need to merge multiple CSV file in a folder, in which this CSV files have same headers and different data, but in some cases two or more columns may have same data.我需要在一个文件夹中合并多个 CSV 文件,其中这个 CSV 文件具有相同的标题和不同的数据,但在某些情况下,两列或多列可能具有相同的数据。

So here is my code,所以这是我的代码,

$filevalues = Import-Csv -Path  (Get-ChildItem -Path C:\Users\hi\ -Filter '*.csv').FullName

$firstfile = Import-Csv -Path C:\Users\hi\VM1.csv

[String[]]$NamesofApplication = $firstfile.Application;
[String[]]$NamesofFolder = $firstfile.FileName;
[String[]]$NamesofConfigvariable = $firstfile.ConfigVariable;
[String[]]$NamesofVM1 = $filevalues.VM1;
[String[]]$NamesofVM2 = $filevalues.VM2;
[String[]]$NamesofVM3 = $filevalues.VM3;

 Write-Host $NamesofVM1.Count

$array = @();
for($i=0; $i -lt $NamesofApplication.Count ; $i++){
    $hashtable = [pscustomobject]@{Application=$NamesofApplication[$i]; FileName=$NamesofFolder[$i]; ConfigVariable=$NamesofConfigvariable[$i];VM1=$NamesofVM1[$i];VM2=$NamesofVM2[$i];VM3=$NamesofVM3[$i] };
    $array += $hashtable;
};

$array | ForEach-Object { [pscustomobject] $_ } | Export-Csv C:\Users\hi\OutPut.csv

and here is my VM1.csv,VM2.csv and VM3.csv VM1.csv , VM2.csv , VM3.csv and here is my VM1.csv,VM2.csv and VM3.csv VM1.csv , VM2.csv , VM3.csv

and i just want my output to be like Output.csv我只希望我的 output 像Output.csv

Can anyone help me in this.任何人都可以在这方面帮助我。

If like in your example, csv can be merge by line, you can add data directly with Add-Member .如果像在您的示例中一样, csv 可以逐行合并,您可以直接使用Add-Member添加数据。 A small example of the use of Add-member with csv data.使用带有 csv 数据的 Add-member 的小示例。

$csv1 = @'
Appli,Folder,FileName,Config,VM1
ABC,Folder1,FN1,Con1,VM11
,Folder2,FN2,Con2,VM12
,Folder3,FN3,Con3,VM13
SID,Folder4,FN4,Con4,VM14
,Folder5,FN5,Con5,VM15
'@ | ConvertFrom-Csv 

$csv2 = @'
Appli,Folder,FileName,Config,VM2
ABC,Folder1,FN1,Con1,VM11
,Folder2,FN2,Con2,VM12
,Folder3,FN3,Con3,VM13
SID,Folder4,FN4,Con4,VM14
,Folder5,FN5,Con5,VM15
'@ | ConvertFrom-Csv 

$csv3 = @'
Appli,Folder,FileName,Config,VM3
ABC,Folder1,FN1,Con1,VM11
,Folder2,FN2,Con2,VM12
,Folder3,FN3,Con3,VM13
SID,Folder4,FN4,Con4,VM14
,Folder5,FN5,Con5,VM15
'@ | ConvertFrom-Csv 


for ($i = 0; $i -lt $csv1.Count; $i++)
{ 
    $csv1[$i] | Add-Member -MemberType NoteProperty -Name VM2 -Value $csv2[$i].VM2
    $csv1[$i] | Add-Member -MemberType NoteProperty -Name VM3 -Value $csv3[$i].VM3
}
$csv1 | ConvertTo-Csv -NoTypeInformation

Try Below Script to Script to merge multiple CSV files into one CSV, by appending each file to the end.尝试使用以下脚本到脚本将多个 CSV 文件合并为一个 CSV,方法是将每个文件附加到末尾。

$getFirstLine = $true $getFirstLine = $true

get-childItem "*.csv" |获取子项 "*.csv" |

foreach { $filePath = $_ foreach { $filePath = $_

$lines = Get-Content $filePath  
$linesToWrite = switch($getFirstLine) {
       $true  {$lines}
       $false {$lines | Select -Skip 1}

}


Add-Content "<Location>\<Output File Name>.csv" $linesToWrite

}

Write-Output "New CSV File successfully created"

Using this Join-Object cmdlet (see also: In Powershell, what's the best way to join two tables into one? ):使用此Join-Object cmdlet(另请参阅:在 Powershell 中,将两个表合并为一个的最佳方法是什么? ):

If there is no relation between the object list (other the the row index):如果 object 列表(其他行索引)之间没有关系:

$csv1 | Merge-Object $csv2 | Merge-object $csv3 | ConvertTo-Csv -NoTypeInformation

But I would advice against this approach as the rows need to be in the same order and have an equal starting point.但是我建议不要使用这种方法,因为行需要以相同的顺序并且具有相同的起点。 It is better to merge the lists on a related item, eg the FileName :最好合并相关项目上的列表,例如FileName

$csv1 | Merge-Object $csv2 -on FileName | Merge-object $csv3 -on FileName | ...

Or multiple properties as Folder and FileName :或多个属性,如FolderFileName

$csv1 | Merge-Object $csv2 -on Folder,FileName | Merge-object $csv3 -on Folder,FileName | ...

All three command will have the same results:所有三个命令都将产生相同的结果:

Appli Folder  FileName Config VM1  VM2  VM3
----- ------  -------- ------ ---  ---  ---
ABC   Folder1 FN1      Con1   VM11 VM11 VM11
      Folder2 FN2      Con2   VM12 VM12 VM12
      Folder3 FN3      Con3   VM13 VM13 VM13
SID   Folder4 FN4      Con4   VM14 VM14 VM14
      Folder5 FN5      Con5   VM15 VM15 VM15

only with the latter the rows do not have to be in order:只有后者的行不必按顺序排列:

暂无
暂无

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

相关问题 使用PowerShell合并两个csv文件(头文件和数据文件) - Merge two csv files using PowerShell (header file and data file) 如何使用PowerShell合并两个csv文件 - How to merge two csv files with PowerShell 如何使用Powershell将列插入单个目录中的多个CSV文件 - How to Insert columns into multiple CSV files in a single directory using powershell 我正在尝试将两个 csv 文件与 powershell 中的不同列合并,接收错误 - I am trying to merge two csv files with different columns in powershell, receiving error 在Powershell中按列顺序合并两个csv列 - Merge two csv columns in powershell, order of columns 如何在PowerShell中将多个对象导出到不同的CSV列 - How can I export multiple objects to different CSV columns in PowerShell 如何使用Powershell在CSV文件中添加数据,插入行和删除列,然后保存? - How can I add data, insert rows and delete columns in a CSV file using powershell and then save it? 如何使用Powershell进行两个CSV文件的多对多合并? - How to do a many to many merge of two CSV files with Powershell? 如何使用 Powershell 将带有元数据标题行的 CSV 文件转换为平面表格? - How can I convert CSV files with a meta data header row into flat tables using Powershell? 如何使用 Powershell 比较两个 CSV 文件,然后使用比较结果更新其中一个 CSV 文件 - How can I use Powershell to compare two CSV files, then update the one of the CSV files with results of comparison
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM