[英]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
:或多个属性,如
Folder
和FileName
:
$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.