【问题标题】:Filter CSV where column is greater or equal to a number过滤列大于或等于数字的 CSV
【发布时间】:2019-02-28 23:18:45
【问题描述】:

我有以下 CSV 文件:

"Count";"Computername";"Files and paths"
"3";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"1";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
"5";"Computer3";"\\SRV\TOTO$\HELLO.BAT"
"8";"Computer4";"\\192.168.8.18\TOTO\DNS.BAT"
"10";"Computer15";"%OSDRIVE%\Hello.exe"
"12";"Computer6";"\\SRV\SCRIPTS\REBOOT.BAT"
"88";"Computer7";"%OSDRIVE%\Winword.exe"
"154";"Computer2";"%OSDRIVE%\excel.exe"

我想保留“计数”大于或等于 8 的所有行。

我尝试了以下命令:

Import-Csv -Path MyFile.csv -Delimiter ";" | Where-Object {$_.Count -ge 8}

但它只返回给我 8 或 88 或 18 的行...而不是所有其他行高于 8 (如 10、12、154 ...)。

为什么?

【问题讨论】:

    标签: powershell int filtering import-csv


    【解决方案1】:

    除非嵌入类型信息 (see documentation),否则每个值都会变成字符串。这导致按字母顺序比较而不是整数一。请参阅以下内容:

    $csv = ConvertFrom-Csv @'
    "Count";"Computername";"Files and paths"
    "3";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
    "1";"Computer1";"%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1"
    "5";"Computer3";"\\SRV\TOTO$\HELLO.BAT"
    "8";"Computer4";"\\192.168.8.18\TOTO\DNS.BAT"
    "10";"Computer15";"%OSDRIVE%\Hello.exe"
    "12";"Computer6";"\\SRV\SCRIPTS\REBOOT.BAT"
    "88";"Computer7";"%OSDRIVE%\Winword.exe"
    "154";"Computer2";"%OSDRIVE%\excel.exe"
    '@ -Delimiter ';'
    
    $csv | gm
    

    所有属性都是字符串:

       TypeName: System.Management.Automation.PSCustomObject
    
    Name            MemberType   Definition                                                                      
    ----            ----------   ----------                                                                      
    Equals          Method       bool Equals(System.Object obj)                                                  
    GetHashCode     Method       int GetHashCode()                                                               
    GetType         Method       type GetType()                                                                  
    ToString        Method       string ToString()                                                               
    Computername    NoteProperty string Computername=Computer1                                                   
    Count           NoteProperty string Count=3                                                                  
    Files and paths NoteProperty string Files and paths=%OSDRIVE%\USERS\0000008\APPDATA\LOCAL\TEMP\__PSSCRIPT.PS1
    

    最简单的解决方案是使用强制转换:

    $csv | Where-Object {[int]$_.Count -ge 8}
    

    【讨论】:

    • 详细说明 PowerShell 尝试将比较的右侧转换为左侧的类型,因此颠倒顺序和逻辑 - 这将起作用:$csv | Where-Object {8 -le $_.Count }
    【解决方案2】:

    Count 被读取为字符串,因此您需要将其更改为整数才能进行比较。

    一种方法是将您的 Where-Object 语句更改为

    where {($_.Count -as [int]) -ge 8}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 2014-08-07
      • 2016-12-27
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多