我有两个我想比较的CSV文件。它们都有多列不同的数据,但它们都有一个包含IP地址的列。称他们为$ log1和$ log2
我正在尝试比较这些文件。如果在$ log2中找到来自$ log1的IP,我想要一个输出文件,其中包含来自$ log2匹配的整行 ...
当我使用时:
Compare-Object -Property 'IP address' -ReferenceObject $log1 -DifferenceObject $log2
它只返回'IP地址'列和SideIndicator。
我想我在这里咆哮错误的树,有人可以提供一些建议吗?
答案 0 :(得分:0)
我会尝试另一种方法:
$log1,$log2 | group-object -Property 'IP address' | where {$_.count -eq 2}
在结果中,您将找到包含两行的group
属性。
答案 1 :(得分:0)
"Try adding the -PassThru flag to your command." - Dave Sexton
This works. I exported the results to CSV and sorted by the SideIndicator when I opened the file (don't think you can get PS to sort by SideIndicator).
Thanks Dave.
There's probably more ways to accomplish this, as noted by others but this achieved my goal.
答案 2 :(得分:0)
此脚本将比较您找到的每个双ip地址的csv文件和写入输出。
#import your csv files
$csv1 = Import-Csv "C:\Users\Admin\Desktop\csv\csv1.csv"
$csv2 = Import-Csv "C:\Users\Admin\Desktop\csv\csv2.csv"
#Compare both csv files (.ip corresponds to your column name for your ip address in the csv file )
$compare = Compare-Object $csv1.ip $csv2.ip -IncludeEqual | ? {$_.SideIndicator -eq "=="}
if ($compare) {
foreach ($ip in $compare.InputObject) {
Write-Output "IP $ip exist in both csv files"
}
}
Else
{
Write-Output "Double ip not found"
}