gpt4 book ai didi

file - Powershell:合并 2 个 CSV 文件中的选择性列

转载 作者:行者123 更新时间:2023-12-02 23:47:05 24 4
gpt4 key购买 nike

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

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

希望寻求一些指导。

谢谢你。

最佳答案

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

对于最后一部分,您还可以使用这些技术中的任何一种 The Many Ways to Create a Custom Object为了创建自定义对象,我选择了“Select-Object”方法,因为您只需重建来自第一个文件的对象位(以更复杂的语法为代价)。

如果您在 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

关于file - Powershell:合并 2 个 CSV 文件中的选择性列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com