Powershell中的数组减法问题
我有两个CSV,如下所示:Powershell中的数组减法问题,powershell,powershell-2.0,Powershell,Powershell 2.0,我有两个CSV,如下所示: A 20180809000 20180809555 20180809666 20180809777 20180809888 $a1= Import-Csv -Path $file1 | select A $a2 = Import-Csv -Path $file2 | select A $a1| where {$a2 -notcontains $_} $a1.A.where{$_ -notin $a2.A} 文件2: A 20180809000 20180
A
20180809000
20180809555
20180809666
20180809777
20180809888
$a1= Import-Csv -Path $file1 | select A
$a2 = Import-Csv -Path $file2 | select A
$a1| where {$a2 -notcontains $_}
$a1.A.where{$_ -notin $a2.A}
文件2:
A
20180809000
20180809555
20180809666
20180809777
我想找出File1和File2的区别,它们应该输出201809888
。我尝试了以下方法:
A
20180809000
20180809555
20180809666
20180809777
20180809888
$a1= Import-Csv -Path $file1 | select A
$a2 = Import-Csv -Path $file2 | select A
$a1| where {$a2 -notcontains $_}
$a1.A.where{$_ -notin $a2.A}
但它会输出整个文件1:
A
--------------
20180809000
20180809555
20180809666
20180809777
20180809888
我也尝试了交叉,但输出为空。您的最后一行应该是:
A
20180809000
20180809555
20180809666
20180809777
20180809888
$a1= Import-Csv -Path $file1 | select A
$a2 = Import-Csv -Path $file2 | select A
$a1| where {$a2 -notcontains $_}
$a1.A.where{$_ -notin $a2.A}
要保留该列,可以对最后一行执行以下操作:
$a1.where{$_.A -notin $a2.A}
$a1 | compare $a2 | select -expand inputobject
这种情况的问题是,如果第二个文件的数据比第一个文件的数据多。然后,您需要在最后一行中执行以下操作:
$a1.where{$_.A -notin $a2.A}
$a1 | compare $a2 | select -expand inputobject
最后一行应该是以下内容:
A
20180809000
20180809555
20180809666
20180809777
20180809888
$a1= Import-Csv -Path $file1 | select A
$a2 = Import-Csv -Path $file2 | select A
$a1| where {$a2 -notcontains $_}
$a1.A.where{$_ -notin $a2.A}
要保留该列,可以对最后一行执行以下操作:
$a1.where{$_.A -notin $a2.A}
$a1 | compare $a2 | select -expand inputobject
这种情况的问题是,如果第二个文件的数据比第一个文件的数据多。然后,您需要在最后一行中执行以下操作:
$a1.where{$_.A -notin $a2.A}
$a1 | compare $a2 | select -expand inputobject
select A
仍将返回一个名为A
的属性的对象
# Returns an object list with property A
Import-Csv -Path $file | select A # (shorthand for Select-Object -Property A)
# A
# ---
# value1
# value2
# ...
您可以使用点符号获取属性A
的值数组,例如:
# Returns the list of values of the A property
(Import-Csv -Path $file).A
# value1
# value2
# ...
以下方面应起作用:
$a1= (Import-Csv -Path $file1).A
$a2 = (Import-Csv -Path $file2).A
$a1 | where {$a2 -notcontains $_}
select A
仍将返回一个名为A
的属性的对象
# Returns an object list with property A
Import-Csv -Path $file | select A # (shorthand for Select-Object -Property A)
# A
# ---
# value1
# value2
# ...
您可以使用点符号获取属性A
的值数组,例如:
# Returns the list of values of the A property
(Import-Csv -Path $file).A
# value1
# value2
# ...
以下方面应起作用:
$a1= (Import-Csv -Path $file1).A
$a2 = (Import-Csv -Path $file2).A
$a1 | where {$a2 -notcontains $_}
最简单的解决方案是使用:
> Compare-Object (Get-Content .\File1.csv) (Get-Content .\File2.csv) -PassThru
20180809888
或者使用导入Csv
> Compare-Object (Import-Csv .\File1.csv).A (Import-Csv .\File2.csv).A -Passthru
20180809888
或
最简单的解决方案是使用:
> Compare-Object (Get-Content .\File1.csv) (Get-Content .\File2.csv) -PassThru
20180809888
或者使用导入Csv
> Compare-Object (Import-Csv .\File1.csv).A (Import-Csv .\File2.csv).A -Passthru
20180809888
或