导入单列CSV文件并将它们加入到单三列CSV文件Powershell中 [英] Import Single Column CSV Files and join them into Single Three Column CSV File Powershell
问题描述
我首先要说我根本不是PowerShell专家,我可以将所有内容拼凑成行,但仅此而已.
I should start by saying I'm no PowerShell expert at all, I can cobble stuff together OK but that's about it.
我有3个.csv文件,都包含1列信息.
I have 3 .csv files that all contain 1 column of information.
我需要一个PowerShell脚本,该脚本将合并单个CSV文件中的每一列,并将它们合并为一个3列CSV文件.
I need a PowerShell script that will combine each column from the individual CSV files and combine them into a single 3 column CSV file.
即
CSV1
Red
Green
Blue
CSV2
Monday
Tuesday
CSV3
Monkey
Badger
Tiger
Giraffe
与之相结合的样子
CSV1 CSV2 CSV3
Red Monday Monkey
Green Tuesday Badger
Blue Tiger
Giraffe
到目前为止,我已经看过创建一个对象并添加成员,但是似乎每个成员只能有一个值.
So far I have looked creating an object and adding members, but it seems that each member can only have one value.
$csv1 = Import-Csv "csv1.csv"
$csv2 = Import-Csv "csv2.csv"
$csv3 = Import-Csv "csv3.csv"
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Rule Block 1" -Value $csv1[2] $csv1[3]
$obj | Add-Member -MemberType NoteProperty -Name "Rule Block 2" -Value $csv2[2]
$obj | Add-Member -MemberType NoteProperty -Name "Rule Block 3" -Value $csv3[2]
$obj | Export-Csv "C:\Users\Simon\Desktop\FJ Stuff\Powershell\exportTest.csv" -NoTypeInformation
我什至不确定我是否正朝着这个方向前进.
I'm not even sure if I'm heading in the write direction with this.
这里的任何帮助将不胜感激.
Any help here would be greatly appreciated.
推荐答案
您可以使用以下方法解决此问题:
You could solve this by using:
$file1 = @(gc "x")
$file2 = @(gc "y")
$file3 = @(gc "z")
$combined = @()
for ($i=0; $i -lt $file1.Count; $i++) {
$combined += $file1[$i] + ', ' + $file2[$i] + ', ' + $file3[$i]
}
$combined | Out-File "C:\test.csv" -encoding default
只需将x/y/z替换为CSV的位置,即可将它们合并到测试csv中,如下所示:
Simply replace the x/y/z with the locations of the CSV, this should combine them into the test csv and look like:
标题-标题-标题
title - title - title
x-y-z
x-y-z
x-y-z
x-y-z
这篇关于导入单列CSV文件并将它们加入到单三列CSV文件Powershell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!