我有 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
其他方法:
#load files
$F1=import-csv "C:tempFile1.txt"
$F2=import-csv "C:tempFile2.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}}
@PetSerAl是正确的,您似乎想将它们合并到行号上,即每行的索引。下面是一个带有自定义对象的人为示例,只需将 $File 1 和 $File 2 替换为 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
}
}