比较对象不返回任何PowerShell

时间:2018-11-26 14:34:53

标签: powershell compare powershell-v2.0 powershell-v3.0

因此,我有两个带有列表的文件“ file1.txt”和“ file2.txt”,例如:

文件1
番茄
番茄酱
芥末酱

文件2:
番茄
芥末

然后我需要采用仅在第一个文件上的行,所以我尝试了

Compare-Object $(Get-Content file1.txt) $(Get-Content file2.txt)

它仍然显示所有行,所以我选择了Property

Compare-Object $(Get-Content file1.txt) $(Get-Content file2.txt) -Property inputObject

我得到了这个结果:

inputObject SideIndicator
----------- -------------
            <=           

我被困在网上,从网上搜索中获得的唯一结果就是人们在添加了“ -Property”过滤器后正确无误。

谢谢!

1 个答案:

答案 0 :(得分:0)

所以我有两个问题(感谢@ Olaf,@ Lee_Dailey和@JosefZ的帮助!)

第一个问题是我要从命令创建的列表中获取数据,因此存在很多空格,这些空格可以通过运行以下命令来解决:

(gc file1.txt) | ? {$_.trim() -ne "" } | set-content file1.txt
(gc file2.txt) | ? {$_.trim() -ne "" } | set-content file2.txt

(gc file1.txt) | ? {$_.trim() -ne " " } | set-content file1.txt
(gc file2.txt) | ? {$_.trim() -ne " " } | set-content file2.txt

然后,我需要执行此操作(由@JosefZ建议)以获取正确的结果集:

(Compare-Object $(Get-Content file1.txt) $(Get-Content file2.txt)|Where-Object {$_.SideIndicator -eq '<='}).InputObject

真的很感谢你们三个,我一个人永远都不可能得到答案!