【问题标题】:Powershell filter a List by Name and DatePowershell 按名称和日期过滤列表
【发布时间】:2020-06-18 06:32:11
【问题描述】:

我需要一些帮助...我是 powershell 新手,我想过滤列表 (csv)。我很想删除其中包含某些名称的所有行。并将列表缩减到上个月。在脚本中,您可以看到我到目前为止走了多远。

param(

    [Parameter(ValueFromPipeline=$true,HelpMessage="Enter CSV path(s)")]
    [String[]]$Path = $null
)



if($Path -eq $null) {

    Add-Type -AssemblyName System.Windows.Forms

    $Dialog = New-Object System.Windows.Forms.OpenFileDialog
    $Dialog.InitialDirectory = "$InitialDirectory"
    $Dialog.Title = "Select CSV File(s)"
    $Dialog.Filter = "CSV File(s)|*.csv"        
    $Dialog.Multiselect=$true
    $Result = $Dialog.ShowDialog()

    if($Result -eq 'OK') {

        Try {

            $Path = $Dialog.FileNames
        }

        Catch {

            $Path = $null
            Break

        }
    }

    else {


        Write-Host -ForegroundColor Yellow "Notice: No file(s) selected."
        Break
    }
}

$info=Import-Csv "$path" -Delimiter ';'
$info | Get-Member 
$info | Format-Table

如您所见,我尝试将路径链接到文件浏览器。

【问题讨论】:

  • “名称”是什么意思?你是指文件中的数据列还是只是标题?
  • 显示包含您的第一个文件以及您期望的输出。
  • 是的,我的意思是那些数据列
  • 如果您想从该对象检索属性以对所述属性进行进一步处理,请不要使用format-table。它应该仅用于显示目的。
  • 很抱歉,但我无法显示此文件...但作为输出,我希望得到同一张表而没有几个名称。

标签: excel powershell csv


【解决方案1】:

出于讨论的目的,我假设 CSV 的完整路径名在变量 $InputPath 中,并且您希望将结果写入完整路径名在变量 $OutputPath 中的 CSV 文件。我还将假设 CSV 文件包含名为“名称”的列,并且您要排除的名称列中的值位于变量 $ExcludedName 中。鉴于此,您可以简单地做

Import-CSV -Path $InputPath | Where-Object {$_.Name -ne $ExcludedName} | Export-CSV -Path $OutputPath -NoTypeInformation

【讨论】:

  • 如果$ExcludedName 是要排除的多个名称的数组,请执行Where-Object {$_.Name -notin $ExcludedName}(我认为这是OP 想要做的......)(+1)
【解决方案2】:

你可以通过我的代码做到这一点,但不要忘记第一行必须包含列名并且分隔符必须是';' $nameslist 是您需要删除的名称数组:

$info=Import-Csv "D:\testdir\file2.csv" -Delimiter ';'
$nameslist=@('James','John','andrew')
foreach($i in $info){
    if($nameslist -contains $i.Name){
    $i.Name=""    
    }
    $i|Export-Csv -Path "D:\testdir\file1.csv"  -Delimiter ';' -NoTypeInformation -Force -Encoding UTF8 -Append   
}

【讨论】:

  • 你现在如何删除名称所在的完整行?
【解决方案3】:

试试这个:

$data = Import-Csv "Path" | Select-Object * -ExcludeProperty Names 
$data | export-csv "Path" -Notype

这将删除列名。

【讨论】:

    【解决方案4】:

    不使用函数先试试:

    
    Import-Csv <Filename> | Where-Object {$_.<FieldName> -notlike "*<Value>*"}
    

    另外,您可能会考虑这样的事情:

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, HelpMessage = "Enter CSV path(s)")]
        [String[]]$Path = $(
            Add-Type -AssemblyName System.Windows.Forms
            $DialogProperties = @{
                Title       = 'Select CSV File(s)'
                Filter      = 'CSV File(s)|*.csv'
                Multiselect = $True
            }
    
            $Dialog = New-Object System.Windows.Forms.OpenFileDialog -Property $DialogProperties
            $Dialog.ShowDialog()
    
            If ($Result -eq 'OK') {
                $Path = $Dialog.FileNames
            } Else {
                Write-Error 'Notice: No file(s) selected.'
            }
        )
    )
    
    Process {
        ForEach ($PathItem in $Path) {
            Import-Csv $PathItem | Where-Object { $_.Name -notlike "*NotThisOne*" }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 2015-11-26
      • 2015-12-15
      相关资源
      最近更新 更多