Windows PowerShell:如何比较两个CSV文件并输出仅在文件中的两个行,而不是在两个文件中

发布于 2025-01-22 16:54:55 字数 904 浏览 0 评论 0原文

我有一个文件 first.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Alpha,Fan,120,jefferson,Riverside,NJ,8075

second.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Bravo,Tan,220,Phila,Riverside,PA,9119

我想比较两个 first.csv second.csv 文件并输出 first的行。

因此, output.csv 应该有

Alpha,Fan,120,jefferson,Riverside,NJ,8075
Bravo,Tan,220,Phila,Riverside,PA,9119

很多类似的问题,但是输出并不是我想要的。

谢谢

I have a file first.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Alpha,Fan,120,jefferson,Riverside,NJ,8075

and second.csv

name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Bravo,Tan,220,Phila,Riverside,PA,9119

I want to compare the rows of both first.csv and second.csv files and output the rows that are either in first.csv or second.csv but not in both.

So the output.csv should have

Alpha,Fan,120,jefferson,Riverside,NJ,8075
Bravo,Tan,220,Phila,Riverside,PA,9119

There are quite a few similar questions but the output is not exactly what I want.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2025-01-29 16:54:55
$filea = Import-Csv C:\Powershell\TestCSVs\group1.csv
$fileb = Import-Csv C:\Powershell\TestCSVs\group2.csv

Compare-Object $filea $fileb -Property name, surname, height, city, county, state, zipCode | Select-Object name, surname, height, city, county, state, zipCode | export-csv C:\Powershell\TestCSVs\out.csv -NoTypeInformation

我正在使用所有字段来在此处进行比较和排序,但是您可以指定要使用以匹配行的唯一值。

输出

"name","surname","height","city","county","state","zipCode"     
"Bravo","Tan","220","Phila","Riverside","PA","9119"             
"Alpha","Fan","120","jefferson","Riverside","NJ","8075"
$filea = Import-Csv C:\Powershell\TestCSVs\group1.csv
$fileb = Import-Csv C:\Powershell\TestCSVs\group2.csv

Compare-Object $filea $fileb -Property name, surname, height, city, county, state, zipCode | Select-Object name, surname, height, city, county, state, zipCode | export-csv C:\Powershell\TestCSVs\out.csv -NoTypeInformation

I'm using the all the fields to compare and sort here but you can specify the unique value(s) that you're wanting to use to match the rows.

output

"name","surname","height","city","county","state","zipCode"     
"Bravo","Tan","220","Phila","Riverside","PA","9119"             
"Alpha","Fan","120","jefferson","Riverside","NJ","8075"
菊凝晚露 2025-01-29 16:54:55

获取对称差异(从两个列表中的所有无关)实际上是一个很普遍的用途比较对象。
因此,我添加了此功能(#30 ) =“ https://www.powershellgallery.com/packages/join” rel =“ nofollow noreferrer”> join-object script / join-object module (另请参阅:在PowerShell中,将两个桌子加入一张桌子的最佳方法是什么?)。

为此,这个特定的问题:

PS C:\> Import-Csv .\First.csv |OuterJoin (Import-Csv .\Second.csv) |Format-Table

name  surname height city      county    state zipCode
----  ------- ------ ----      ------    ----- -------
Alpha Fan     120    jefferson Riverside NJ    8075
Bravo Tan     220    Phila     Riverside PA    9119

Getting the symmetric difference (everything that is unrelated) from two lists is actually a quite common use in comparing objects.
Therefore, I have added this feature (#30) to the Join-Object script/Join-Object Module (see also: In Powershell, what's the best way to join two tables into one?).

For this for this specific question:

PS C:\> Import-Csv .\First.csv |OuterJoin (Import-Csv .\Second.csv) |Format-Table

name  surname height city      county    state zipCode
----  ------- ------ ----      ------    ----- -------
Alpha Fan     120    jefferson Riverside NJ    8075
Bravo Tan     220    Phila     Riverside PA    9119
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文