1.5 年后,我在原始答案中粘贴的 cmdlet 经历了多次更新,以至于它已经完全过时了。因此我更换了代码和自述文件 https://github.com/iRon7/Join-Object并附有最新版本的链接。
连接对象 https://www.powershellgallery.com/packages/JoinModule
根据两个对象列表之间的相关属性组合它们。
描述
组合一个或多个对象的属性。它创建一个可以保存为新对象或按原样使用的集合。对象连接是一种通过使用每个对象列表中公共的值来组合来自一个(自连接)或多个对象列表的属性的方法。
主要特点
- 直观、惯用的 PowerShell 语法
- 类似 SQL 的连接功能
- 智能财产合并
- 用于更新、合并和特定连接类型的预定义连接命令
- 为(左)输入对象和输出对象定义良好的管道(正确使用时保留内存)
- 在大型对象列表上执行速度大约是 Compare-Object 的两倍
- 支持(自定义)对象、数据表和字典(例如哈希表)作为输入
- 智能属性和计算属性表达式
- 自定义关系表达式
- 安装方便(点采购)
- 支持 PowerShell for Windows (5.1) 和 PowerShell Core
Join-Object cmdlet 显示以下代理命令及其自己的 (-JoinType
and -Property
) 默认值:
-
InnerJoin-Object
(Alias InnerJoin
or Join
),组合相关对象
-
LeftJoin-Object
(Alias LeftJoin
),合并相关对象并添加剩余的左侧对象
-
RightJoin-Object
(Alias RightJoin
),组合相关对象并添加其余正确的对象
-
FullJoin-Object
(Alias FullJoin
),合并相关对象并添加其余左右对象
-
CrossJoin-Object
(Alias CrossJoin
),将每个左侧对象与每个右侧对象组合
-
Update-Object
(Alias Update
),用相关的右对象更新左对象
-
Merge-Object
(Alias Merge
),用相关的右对象更新左对象,并添加其余新的(不相关的)右对象
ReadMe https://github.com/iRon7/Join-Object
完整的自述文件(和源代码)可从 GitHub 获取:https://github.com/iRon7/Join-Object https://github.com/iRon7/Join-Object
安装
这个有两个版本Join-Object
cmdlet(两个版本提供相同的功能):
- 加入模块 https://www.powershellgallery.com/packages/JoinModule
Install-Module -Name JoinModule
- 加入脚本 https://www.powershellgallery.com/packages/Join
Install-Script -Name Join
(或重命名Join.psm1
模块到一个Join.ps1
脚本文件)
并通过调用脚本点源 https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-6#script-scope-and-dot-sourcing:
. .\Join.ps1
Answer
回答问题中的实际示例:
$reservations |LeftJoin $leases -On IP
IP MAC Name
-- --- ----
192.168.1.1 001D606839C2 Apple
192.168.1.2 00E018782BE1 Pear
192.168.1.3 0022192AF09C Banana
192.168.1.4 0013D4352A0D
表现
关于绩效衡量的一点说明:
PowerShell 管道旨在stream对象(节省内存),这意味着两个列表输入对象通常不是(不应该)驻留在记忆中。通常它们是从其他地方(即远程服务器或磁盘)检索的。还,输出通常matters where linq https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/解决方案速度很快,但很容易让您在得出结论时陷入困境,因为linq
字面上地defers执行(惰性评估 https://en.wikipedia.org/wiki/Lazy_evaluation), 也可以看看:从数组的属性获取唯一索引项的最快方法 https://stackoverflow.com/a/59437162/1701026.
换句话说,如果涉及(测量)PowerShell 的性能,重要的是要关注完整的端到端解决方案,这更有可能看起来像:
import-csv .\reservations.csv |LeftJoin (import-csv .\leases.csv) -On IP |Export-Csv .\results.csv
(1) Note: unfortunately, there is no easy way to build two parallel input streams (see: #15206 Deferred input pipelines https://github.com/PowerShell/PowerShell/issues/15206)
(更多)示例
更多示例可以在相关的 Stack Overflow 问题中找到:
- 合并多个 CSV 文件 https://stackoverflow.com/a/54855458/1701026
- CMD 或 Powershell 命令组合(合并)两个文件中的相应行 https://stackoverflow.com/a/54607741/1701026
- 我可以在 powershell 中的对象上使用 SQL 命令(例如 join),而不涉及任何 SQL 服务器/数据库吗? https://stackoverflow.com/a/55431393/1701026
- Powershell 匹配属性,然后有选择地组合对象以创建第三个 https://stackoverflow.com/a/54855647/1701026
- 比较两个 CSV,匹配 2 个或更多列上的列,使用 powershell 从两个 CSV 中导出特定列 https://stackoverflow.com/a/52235645/1701026
- 合并两个 CSV 文件,同时添加新条目并覆盖现有条目 https://stackoverflow.com/a/54949056/1701026
- 合并两个 CSV,然后对输出中的列重新排序 https://stackoverflow.com/a/54981257/1701026
- 有效合并具有多个匹配键的大型对象数据集 https://stackoverflow.com/a/55543321/1701026
- 如何比较两个 CSV 文件并输出其中一个文件中但不在两个文件中的行 https://stackoverflow.com/a/72012355/1701026
- 如何使用 SQL LIKE 语法在 Powershell 中连接两个 CSV 文件 https://stackoverflow.com/a/73712540/1701026
并排连接示例
- 合并两个 CSV - 将 CSV 添加为另一列 https://stackoverflow.com/a/55431240/1701026
- 是否有相当于的 PowerShellpaste(即水平文件串联)? https://stackoverflow.com/a/68070763/1701026
- 如何将3个循环合并并导出到一张表中 https://stackoverflow.com/a/74812558/1701026
- 在powershell中将两个数组对象合并为一个数组对象 https://stackoverflow.com/q/75331465/1701026
- 在 PowerShell 中将 XML 转换为 CSV 时,是否有办法将水平格式的 CSV 输出转换为垂直格式? https://stackoverflow.com/a/76330651/1701026
- 如何将此 Json 数据作为平面对象而不是嵌套对象导入 https://stackoverflow.com/a/77022611/1701026
并且在连接对象测试脚本 https://github.com/iRon7/Join-Object/blob/master/Tests/Join-Object.Tests.ps1.
Please give a ???? if you support the proposal to Add a Join-Object cmdlet to the standard PowerShell equipment (#14994) https://github.com/PowerShell/PowerShell/issues/14994