如何在powershell中比较两个csv文件而不指定属性 [英] how to compare two csv files in powershell without specifying properties

查看:328
本文介绍了如何在powershell中比较两个csv文件而不指定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个csv文件:


ipaddress,port
10.140.11.1,80
10.140.11.2,80




ipaddress,port
10.140.11.1,80
10.140.11.2,8008

问题是如何比较powershell中的文件。我已经尝试过:

The question is how to compare the files in powershell. I have already tried this:

$file1 = import-csv "csvfile1.csv"
$file2 = import-csv "csvfile2.csv"
Compare-Object $file1 $file2 -IncludeEqual

结果是那两个文件是相等的。

The result is as those two files were equal.

如果指定特定属性,它会按预期工作,例如:

It works as expected if I specify the particular property, e.g:

Compare-Object $file1 $file2 -IncludeEqual -Property port

如何比较csv文件指定属性。让我们来比较csv文件中的所有属性。

How to compare the csv files without specifying the properties. Let's say I would like to compare all properties in the csv file.

推荐答案

code> Get-Member -MemberType NoteProperty ,然后将该列表传递给 Compare-Object

You can obtain the list of CSV column properties via Get-Member -MemberType NoteProperty, then pass that list to Compare-Object.

# get list of CSV properties
$props1 = $file1 | gm -MemberType NoteProperty | select -expand Name | sort | % {"$_"}
$props2 = $file2 | gm -MemberType NoteProperty | select -expand Name | sort | % {"$_"}

# first check that properties match (can omit this step if you know for sure they will be)
if(Compare-Object $props1 $props2)
{
    throw "Properties are not the same! [$props1] [$props2]"
}
# pass properties list to Compare-Object
else
{
    Compare-Object $file1 $file2 -Property $props1
}

这篇关于如何在powershell中比较两个csv文件而不指定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆