[英]Comparing different columns of 2 csv files
I am trying to compare 2 colums in 2 csv files with eachother so that i can check if there are differences in the 2 files.我正在尝试将 2 个 csv 文件中的 2 个列相互比较,以便我可以检查这 2 个文件中是否存在差异。 Because 1 of the files is a export from a incident management system, the headers that are being used are different.
由于其中 1 个文件是从事件管理系统导出的,因此使用的标头不同。 This means that i have to compare the items under the first header of the first file(Meldings-nummer) to the ones under the second header of the second file, which is Incidentnummer.
这意味着我必须将第一个文件 (Meldings-nummer) 的第一个标题下的项目与第二个文件的第二个标题下的项目进行比较,即 Incidentnummer。
I got it to work if the headers are the same, using following code.如果标题相同,我使用以下代码使其工作。 However, this doesnt work using different headernames and it also doesn´t show the results.
但是,这在使用不同的标题名称时不起作用,也不会显示结果。
$firstFile = read-host - prompt "Please input exact file location of first .csv file"
$secFile = read-host - prompt "Please input exact file location of second .csv file"
$ff = Import-Csv $firstFile
$sf = Import-Csv $secFile
Compare-Object -referenceobject $ff -differenceobject $sf -includeequal |Where-Object {$_.SideIndicator -ne "=="} |Select -ExpandProperty InputObject
Or, using the easier version that shows only meldings-nummer:或者,使用仅显示 meldings-nummer 的更简单版本:
$file1 = import-csv -Path "C:\Users\Documents\scripts\Powershell\Overzicht_Meldingen_2015-10-21.csv"
$file2 = import-csv -Path "C:\Users\Documents\scripts\Powershell\Overzicht_Meldingen_2015-10-21aangepast.csv"
Compare-Object $file1 $file2 -property Meldings-nummer
This is the one that´s preferred, but then with the flexibility of the first one(having to manually enter the file location)这是首选的,但具有第一个的灵活性(必须手动输入文件位置)
Does anyone know how to, or have any tips so i can archieve the comparing of different columns and showing them?有谁知道如何或有任何提示,以便我可以存档不同列的比较并显示它们?
UPDATE I just found some code that i adjusted.更新我刚刚找到了一些我调整过的代码。 I think this should compare the 2 files and give a new file that shows the differences.
我认为这应该比较这两个文件并给出一个显示差异的新文件。 Even though it executes fine and gives me the file, there is nothing in the file except for the text 'Wijziging'.
即使它执行得很好并给了我文件,除了文本“Wijziging”之外,文件中没有任何内容。
$OldCSV = import-csv -Path "C:\Users\Documents\scripts\Powershell\Overzicht_Meldingen_2015-10-21.csv"
$NewCSV = import-csv -Path "C:\Users\Documents\scripts\Powershell\Overzicht_Meldingen_2015-10-21aangepast.csv"
$output = @()
forEach ($Column in $OldCsv) {
$result = $NewCSV | Where-Object {$Column."Meldings-nummer" -eq $_.Incidentnummer}
$Melding = if ($Column.Powerstate -ne $result.PowerState) {"Previous: " + $Column."Meldings-nummer" + " | Now:" + $result.Incidentnummer}
$output += New-object PSObject -property @{
#VMname = $Column.Name
Wijziging = $Melding
}
}
$output | select-object Wijziging | Export-Csv -Path C:\Users\Documents\scripts\Powershell\Changes.csv -NoTypeInformation
thanks for al the help but i think i solved it myself using the following code.感谢您的帮助,但我想我使用以下代码自己解决了这个问题。
$file1 = import-csv -Path "C:\Users\Documents\scripts\Powershell\Overzicht_Meldingen_2015-10-21.csv" -header Meldings-nummer
$file2 = import-csv -Path "C:\Users\Documents\scripts\Powershell\Tweedelijns_incidenten14.csv" -header Incidentnummer
Where-Object {$File1."Meldings-nummer" -ne $file2.Incidentnummer}
Compare-Object $file1 $file2
Now i can put the finishing touch in it so it ask for the csv files like in the first code现在我可以在其中进行最后的润色,因此它会像第一个代码一样要求 csv 文件
Assuming the first file has the Meldings-nummer in ascending sequence and the second file has the Incidentnummer in ascending sequence, you could do it like this:假设第一个文件的 Meldings-nummer 按升序排列,第二个文件的 Incidentnummer 按升序排列,您可以这样做:
[int] $i = 0
[int] $j = 0
while ($i -le $ff.Count)
{
if ($ff[$i].'Meldings-nummer' -lt $sf[$j].IncidentNummer -or $j -gt $sf.Count)
{
Write-Host ("Meldings-nummer {0} not in second file." -f $ff[$i].'Meldings-nummer')
$i++
}
while ($ff[$i].'Meldings-nummer' -gt $sf[$j].IncidentNummer -or $i -gt $ff.Count)
{
if ($j -ge $sf.Count)
{ break }
Write-Host ("IncidentNummer {0} not in first file." -f $sf[$j].IncidentNummer)
$j++
}
if ($ff[$i].'Meldings-nummer' -eq $sf[$j].IncidentNummer)
{
$j++
$i++
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.