如果文件不会太大,我会使用公共列值作为键将一个加载到哈希表中,然后遍历第二个文件并使用键值从第一个文件中查找要合并的列。如果第一个文件很大(大小取决于您拥有多少 RAM),您只需要注意占用过多的 RAM,因为它的全部内容将被加载到内存中。
#Make an empty hash table for the first file
$File1Values = @{}
#Import the first file and save the rows in the hash table indexed on "KeyColumn"
Import-Csv -Path c:\file1.csv | ForEach-Object {
$File1Values.Add($_.KeyColumn, $_)
}
#Import the second file, using Select-Object to select all the values from file2,
# and adding custom properties from the first file using the name/expression
# hash tables.
Import-Csv -Path c:\file2.csv | Select-Object *,@{
Name="ABC"; Expression={$File1Values[$_.KeyColumn].ABC}
}, @{
#You can abbreviate Name/Expression
N="DEF"; E={$File1Values[$_.KeyColumn].DEF}
} | Export-Csv -Path c:\OutFile.csv
对于最后一部分,您还可以使用这些技术中的任何一种创建自定义对象的多种方法来创建自定义对象,我选择了“选择对象”方法,因为您只需重建即将到来的对象的位从第一个文件(以更复杂的语法为代价)。
如果您在 V3 上并且想要使用新的 [PsCustomObject] 类型加速器,最后一点看起来像这样(注意您必须手动添加文件 1 和文件 2 属性):
#Import the second file and make a custom object with properties from both files
Import-Csv -Path c:\file2.csv | ForEach-Object {
[PsCustomObject]@{
ABC = $File1Values[$_.KeyColumn].ABC;
DEF = $File1Values[$_.KeyColumn].DEF;
UVW = $_.UVW;
XYZ = $_.XYZ;
}
} | Export-Csv -Path c:\OutFile.csv