繁体   English   中英

如何使用 PowerShell 过滤 CSV 文件?

[英]How to filter CSV File with PowerShell?

我有一个 CSV 文件用于备份报告,其中我有多个列。 我只想获得那些从未成功使用特定存储集的客户。

示例输入文件是

Client Name,Save Set Name,Group,Status
a,All,Group1,Failed
a,SQL,Group2,succeeded
b,SQL,Group1,Failed
c,FS,Group1,Failed
d,DBA,Group1,Failed
e,RDM,Group1,Failed
a,ALL,Group2,succeeded
b,SQL,Group3,succeeded
c,SQL,Group4, Failed

Output

Client Name Save Set Name   Status  Group
a   All Failed  Group1
b   SQL Failed  Group1
c   FS  Failed  Group1
d   DBA Failed  Group1
e   RDM Failed  Group1
c   SQL Failed  Group4

预计 Output

Client Name,Save Set Name,Group,Status
c,FS,Group1,Failed
d,DBA,Group1,Failed
e,RDM,Group1,Failed
c,SQL,Group4, Failed

在下面的命令中,我面临的问题是,我将客户端设置为在其他组中成功的Failed ,而我只希望那些具有失败且状态没有任何成功值的存储集的客户端。

Get-Content E:\Report\Daily_Failed.csv | 
    ConvertFrom-Csv | 
        Select-Object -Unique * | 
            Group-Object -Property 'Client Name', 'Save Set Name', 'Group' | 
                Where-Object { 0 -eq ($_.Group | Where-Object Status -eq 'succeeded').Count } | 
                    Select-Object -Expand Group | 
                        Select-Object "Client Name", "Save Set Name", "status", "Group" |
                            Export-Csv -NoTypeInformation E:\Report\Failed_$CurrentDate.csv

示例 2。

Client Name,Save Set Name,Group,Status
gsiecwt2020.web.local,pseudo_saveset,D_CWT_File_System_1,failed
gsiecwt2020.web.local,J:\System,D_CWT_File_System_2,succeeded
gsiecwt2020.web.local,K:\System,D_CWT_File_System_1,succeeded
gsiecwt2020.web.local,K:\System,D_CWT_File_System_3,failed

所需 Output

gsiecwt2020.web.local,pseudo_saveset,D_CWT_File_System_1,failed

它应该与Client NameSave Set NameStatus匹配。 如果具有相同Save Set Name的客户端在任何其他组中成功,则应将其标记为成功,但如果具有特定Save Set Name的客户端失败,则应将其标记为Failed

您可以使用Group-ObjectWhere-Object子句来做到这一点。

对于演示,我使用的是 Here-String; 在现实生活中,你会从文件中导入 csv

$csv = Import-Csv -Path 'X:\yourInputFile.csv'

代码:

$csv = @"
Client Name,Save Set Name,Group,Status
a,All,Group1,Failed
a,SQL,Group2,succeeded
b,SQL,Group1,Failed
c,FS,Group1,Failed
d,DBA,Group1,Failed
e,RDM,Group1,Failed
a,ALL,Group2,succeeded
b,SQL,Group3,succeeded
c,SQL,Group4, Failed
"@ | ConvertFrom-Csv

$result = $csv | Group-Object 'Client Name' | ForEach-Object {
    $succeeded = $_.Group | Where-Object { $_.Status -eq 'succeeded' }
    if (!$succeeded) { $_.Group }  # never succeeded, so output these
}

# output on screen
$result | Format-Table -AutoSize

# output to CSV
$result | Export-Csv -Path 'X:\yourFilteredCsv.csv' -UseCulture -NoTypeInformation

结果:

Client Name Save Set Name Group  Status
----------- ------------- -----  ------
c           FS            Group1 Failed
c           SQL           Group4 Failed
d           DBA           Group1 Failed
e           RDM           Group1 Failed

看到您的最新评论,我想/希望我现在更好地理解了这个问题。 幸运的是,代码可以很容易地调整为不仅在“客户端名称”上进行分组,还可以在“保存集名称”上进行分组,如下所示。

$result = $csv | Group-Object 'Client Name','Save Set Name' | ForEach-Object {
    # if any of the group items have a status of 'succeeded', skip that group
    $succeeded = $_.Group | Where-Object { $_.Status -eq 'succeeded' }
    if (!$succeeded) { $_.Group }  # never succeeded, so output these
}

# output on screen
$result | Format-Table -AutoSize

# output to CSV
$result | Export-Csv -Path 'X:\yourFilteredCsv.csv' -UseCulture -NoTypeInformation

使用示例 1:

Client Name,Save Set Name,Group,Status
a,All,Group1,Failed
a,SQL,Group2,succeeded
b,SQL,Group1,Failed
c,FS,Group1,Failed
d,DBA,Group1,Failed
e,RDM,Group1,Failed
a,ALL,Group2,succeeded
b,SQL,Group3,succeeded
c,SQL,Group4, Failed

产量:

Client Name Save Set Name Group  Status
----------- ------------- -----  ------
c           FS            Group1 Failed
d           DBA           Group1 Failed
e           RDM           Group1 Failed
c           SQL           Group4 Failed

使用示例 2:

Client Name,Save Set Name,Group,Status
gsiecwt2020.web.local,pseudo_saveset,D_CWT_File_System_1,failed
gsiecwt2020.web.local,J:\System,D_CWT_File_System_2,succeeded
gsiecwt2020.web.local,K:\System,D_CWT_File_System_1,succeeded
gsiecwt2020.web.local,K:\System,D_CWT_File_System_3,failed

产量:

Client Name           Save Set Name  Group               Status
-----------           -------------  -----               ------
gsiecwt2020.web.local pseudo_saveset D_CWT_File_System_1 failed

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM