Powershell 在没有关键字段的情况下合并来自不同文件的 2 列

Powershell merge 2 columns from different files without a key field

我有 2 个 csv 文件,分别是 file1.column1、file1.column2、file2.column1、file2.column2。我想像下面这样

文件 1:

Column1, Column2
1,a
2,b
3,c

文件 2:

Column1, Column2
x, abc
y, def
z, ghi

我期望的输出是: 文件 3:

File1.column1, File2.column2
1, abc
2, def
3, ghi

@PetSerAl 是正确的,看来您想在行号上合并这些,即每行的索引。这是一个带有自定义对象的人为示例,只需将 $File1 和 $File2 替换为 Import-Csv 的输出:

$File1 = @(
[PSCustomObject]@{
    Column1 = 1
    Column2 = "a"
}
[PSCustomObject]@{
    Column1 = 2
    Column2 = "b"
}
[PSCustomObject]@{
    Column1 = 3
    Column2 = "c"
}
)

$File2 = @(
    [PSCustomObject]@{
        Column1 = "x"
        Column2 = "abc"
    }
    [PSCustomObject]@{
        Column1 = "y"
        Column2 = "def"
    }
    [PSCustomObject]@{
        Column1 = "z"
        Column2 = "ghi"
    }
)

for ($i = 0; $i -lt $File1.Count; $i++ )
{
    [PSCustomObject]@{
        "File1.Column1" = $File1[$i].Column1
        "File2.Column2" = $File2[$i].Column2
    }
}

其他方法:

#load files
$F1=import-csv "C:\temp\File1.txt"
$F2=import-csv "C:\temp\File2.txt"

#found max element count for 2 files
$Count=[System.Math]::Max($F1.Count, $F2.Count)

#print as you want
0..$Count | select @{N="File1.Column1"; E={$F1[$_].Column1}}, @{N="File2.Column2"; E={$F2[$_].Column2}}