如何在 powershell 中比较 2 个 CSV 文件

How to compare 2 CSV files in powershell

我正在尝试将 2 个 CSV 文件与 (logs_all、logs_new) 中的日志信息进行比较 格式:

我想从 logs_new

添加 logs_all 中不存在的行
$csv1 = Import-Csv $csvLogs_all -Delimiter "`t"
$csv2 = Import-Csv $csvLogs_new -Delimiter "`t"

$end     = $csv1.Count
$count   = 0
$diffobj = @()

# testtable is the name of the column, adjust it to your column header name
if($csv1.time[$count] -ne $csv2.time){
   $diffobj += $csv2[$count]
}

$count++
}until($count -eq $end)
Write-Host "chekking logs"
$diffobj | export-csv $csvLogs_all -NoTypeInformation

但这不是很好,有人能帮我吗?

查看 Compare-Object cmdlet。

该 cmdlet 执行以下操作:

The Compare-Object cmdlet compares two sets of objects. One set of objects is the "reference set," and the other set is the "difference set."

The result of the comparison indicates whether a property value appeared only in the object from the reference set (indicated by the <= symbol), only in the object from the difference set (indicated by the => symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).

换句话说,它比较两个对象并告诉您差异。

要比较两个 CSV 文件,您可以 运行

$file1 = import-csv -Path "C:\temp\Test1.csv" 
$file2 = import-csv -Path "C:\temp\Test2.csv" 
Compare-Object $file1 $file2 -property ColumnName -IncludeEqual