【发布时间】:2019-04-30 07:46:30
【问题描述】:
我们必须每天合并一些包含“Computer | Updates_Missing”的 CSV。但是为了保持这个文件更新并且没有重复的计算机,我想创建一个可以合并多个 CSV 并删除重复计算机的脚本,但 仅在以下情况下: 如果计算机是重复的,只保留计算机具有最低结果的行进行更新(或者如果重复导致更新也删除行)
我解释一下:
csv_day_1:
Computer_1 | 12
Computer_2 | 8
Computer_3 | 16
Computer_4 | 7
csv_day_2:
Computer_1 | 4
Computer_2 | 8
Computer_4 | 2
Computer_7 | 22
我希望最终结果是这样的:
Computer_1 | 4
Computer_2 | 8
Computer_3 | 16
Computer_4 | 2
Computer_7 | 22
我想要这样的模式:
-
Import-Csv并选择“计算机”列 - 如果计算机是重复的,请选择“Updates_missing”较少的行并删除其他计算机
- 如果一台计算机多次得到相同的结果,只需保留一行。
这是一个 GUI 脚本,所以看起来像这样...:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,300'
$Form.text = "Merge_CSV"
$Form.TopMost = $false
$Form.MaximizeBox = $false
$Form.FormBorderStyle = 'Fixed3D'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Browse your *.csv Files"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(40,20)
$Label1.Font = 'Arial,10'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Browse..."
$Button1.width = 100
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(60,50)
$Button1.Font = 'Arial,10'
$Button1.Add_Click({
# Browse the files
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $true
Filter = 'CSV Files (*.csv)|*.csv'
}
[void]$FileBrowser.ShowDialog()
$path1 = $FileBrowser.FileNames
foreach ($line in $path1){
$TextBox2.Text += "$line"+"`r`n"
}
})
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 200
$TextBox1.height = 30
$TextBox1.location = New-Object System.Drawing.Point(380,50)
$TextBox1.Font = 'Arial,10'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Name the exported file :"
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.location = New-Object System.Drawing.Point(410,20)
$Label2.Font = 'Arial,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Fusionner et Convertir"
$Button2.width = 200
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(200,110)
$Button2.Font = 'Arial,11,style=bold'
$Button1.Add_Click({
# 1 - Merge the file
$CSV= @();
Get-ChildItem $path1 | ForEach-Object{
$CSV += @(Import-Csv -Delimiter ";" -Path $_)
}
$CSV | Export-Csv -Path C:\Temp\Fusion_CSV.csv -NoTypeInformation -Delimiter ";"
# 2 - Clean the merge
Import-csv C:\Temp\Fusion_CSV.csv -Delimiter ";" | Group-Object -Property "Computer"
})
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $true
$TextBox2.width = 560
$TextBox2.height = 120
$TextBox2.location = New-Object System.Drawing.Point(20,160)
$TextBox2.Font = 'Arial,9'
$Form.controls.AddRange(@($Label1,$Button1,$TextBox1,$Label2,$Button2,$TextBox2))
#endregion GUI }
[void]$Form.ShowDialog()
【问题讨论】:
标签: powershell csv merge duplicates