2

由于突然需要编写一个脚本,将 2 个 csv 文件与至少有 1 个公共列的行和列结合起来,我求助于 powershell。我是 Powershell 的菜鸟。谁能建议如何从两个文件中读取,将行与公共列进行比较和组合,最后输出到另一个文件?

CSV 文件 1

Hosts  ABC  DEF
=====  ===  ===
SVR01   10  100
SRV02   22   99

CSV 文件 2

Hosts  UVW   XYZ
=====  ===   ===
SVR01   13  10.5
SRV02   19   8.9

预期产出

Hosts  DEF  UVW   XYZ
=====  ===  ===   ===
SVR01  100   13  10.5
SRV02   99   19   8.9

希望寻求一些指导。

谢谢你。

4

1 回答 1

3

如果文件不会太大,我会使用公共列值作为键将一个加载到哈希表中,然后遍历第二个文件并使用键值从第一个文件中查找要合并的列。如果第一个文件很大(大小取决于您拥有多少 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
于 2013-07-17T03:51:33.467 回答