gpt4 book ai didi

powershell - CSV中重复行时合并和删除MINIMAL结果

转载 作者:行者123 更新时间:2023-12-02 23:53:09 25 4
gpt4 key购买 nike

我们必须每天合并一些包含“计算机| 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()

    最佳答案

    顺便说一句,这是一个不好的模式:

    $CSV = @();
    Get-ChildItem $path1 | ForEach-Object {
    $CSV += @(Import-Csv -Delimiter ";" -Path $_)
    }

    串联数组非常昂贵,应避免使用,因为无法扩展PowerShell数组。它必须复制内存中的整个数组,并在每次添加新值时附加新数据。

    试试这个:
    $CSV = Get-ChildItem $path1 | Import-Csv -Delimiter ";"
    $CSV = $CSV | Group-Object -Property Computer |
    Select-Object @{Name='Computer';Expression={$_.Name}}, @{Name='Updates_Missing';Expression={ $_.Group | Measure-Object -Minimum -Property Updates_Missing | Select-Object -ExpandProperty Minimum } }

    之后,选择对象将使用计算出的属性来确定缺少的最小更新数。您需要注意丢失或为空的值,因为它们可能会被解释为零。您可能需要使用 Where-Object { -not [String]::IsNullOrWhiteSpace($_.Updates_Missing) }之类的内容将其过滤掉。您还必须注意Updates_Missing列中的所有非数字值。

    计算出的第一个属性 @{Name='Computer';Expression={$_.Name}}只是将 Name列从Group-Object的输出重命名为 Computer。 [注意:您可以只指定 @{n='Computer';e={$_.Name}}。为了清楚起见,我使用了计算所得的属性元素的全名。]

    计算出的第二个属性是计算内容:
    @{Name='Updates_Missing';Expression={ $_.Group | Measure-Object -Minimum -Property Updates_Missing | Select-Object -ExpandProperty Minimum } }

    我们希望第二列的名称为 Updates_Missing。但是,表达式更复杂。组对象输出中的 Group列是组中每个对象的集合。

    这就是仅使用Group-Object就可以看到的测试数据:
    PS C:\> $CSV | Group-Object -Property Computer

    Count Name Group
    ----- ---- -----
    2 Computer_1 {@{Computer=Computer_1; Updates_Missing=12}, @{Computer=Computer_1; Updates_Missing=4}}
    2 Computer_2 {@{Computer=Computer_2; Updates_Missing=8}, @{Computer=Computer_2; Updates_Missing=8}}
    2 Computer_3 {@{Computer=Computer_3; Updates_Missing=16}, @{Computer=Computer_3; Updates_Missing=16}}
    2 Computer_4 {@{Computer=Computer_4; Updates_Missing=7}, @{Computer=Computer_4; Updates_Missing=2}}
    1 Computer_7 {@{Computer=Computer_7; Updates_Missing=22}}

    让我们看一下第一条记录的 Group:
    PS C:\> ($CSV | Group-Object -Property Computer)[0].Group

    Computer Updates_Missing
    -------- ---------------
    Computer_1 12
    Computer_1 4

    它是两个对象的集合。我们可以使用Measure-Object来找到最小值:
    PS C:\> ($CSV | Group-Object -Property Computer)[0].Group | Measure-Object -Property Updates_Missing -Minimum


    Count : 2
    Average :
    Sum :
    Maximum :
    Minimum : 4
    Property : Updates_Missing

    请注意, Measure-Object足够聪明,可以将其获得的字符串输入视为数字值。那可能会咬我们。例如,缺失值可能在输出中显示为零。您需要考虑到这一点。

    我们只需要最小值,而不想要其余的度量对象。所以:
    PS C:\> ($CSV | Group-Object -Property Computer)[0].Group | Measure-Object -Property Updates_Missing -Minimum | Select-Object -ExpandProperty Minimum
    4

    这就是您在第二个计算属性中为表达式得出的结果:
    @{Name='Updates_Missing';Expression={ $_.Group | Measure-Object -Minimum -Property Updates_Missing | Select-Object -ExpandProperty Minimum } }

    如果您有多列,那么事情会变得更加困难。

    假设您的专栏现在为:计算机,IP和Updates_Missing。

    尝试类似:
    $CSV | Group-Object -Property Computer | 
    Select-Object @{Name = 'Computer'; Expression = {$_.Name}},
    @{Name = 'IP' ; Expression = { $_.Group | Sort-Object -Property @{Expression = {[int]$_.Updates_Missing}} | Select-Object -ExpandProperty IP -First 1 } },
    @{Name = 'Updates_Missing'; Expression = { $_.Group | Sort-Object -Property @{Expression = {[int]$_.Updates_Missing}} | Select-Object -ExpandProperty Updates_Missing -First 1 } }

    我再次改变了这里的逻辑。代替使用Measure-Object,我们将使用Sort-Object,将其计算属性与Select-Object组合在一起以仅获取第一条记录。这样,当我们说 Computer_1具有4 Missing_Updates时,我们返回的IP是该记录中缺少4个更新的IP。您可以对后续字段重复相同的逻辑,仅更新属性名称和为 Select-Object -ExpandProperty指定的属性。

    关于powershell - CSV中重复行时合并和删除MINIMAL结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518820/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com