代码之家  ›  专栏  ›  技术社区  ›  Merrill Cook

如何将选择字符串输出从大容量文本文件保存到数组PowerShell

  •  1
  • Merrill Cook  · 技术社区  · 8 年前

    我正在尝试将子字符串从文本文件中大容量地保存到数组中。我尝试过以下的变化。这会将所有选定的字符串输出到屏幕,但只将最终输出保存到变量。有没有办法模拟outvariable中的=+运算符的功能,以便所有项都存储在一个数组中?

    $FILES = ls "*.txt"
    foreach($f in $FILES){
      $in=Get-Content $f
      $in | Foreach { Select-String -Path "$f" -Pattern "Ad ID" -outvariable 
      array1 }}
    

    在我的策略被误导的情况下,将子字符串拉入数组的总体目的是让这些文本文件有几个单独的子字符串数组。然后我将把这些值连接成一个csv。我正在尝试拉出元素,而不是重新排列文本文件,因为文本文件中的子字符串顺序不同。例子:

    TXT文件1:

    Ad Id: xxxx
    Ad Text: blah blah
    Ad placement: spaceship
    

    TXT文件二:

    Ad Id: yyyy
    Ad placement: zoo
    Ad Text: blah blah
    

    最终期望结果(除元素顺序外,此部分工作)

    csv文件

    xxxx, spaceship, blah blah
    yyyy, zoo, blah blah
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   f6a4    8 年前

    试试这个:

    $files      = ls "*.txt"
    $dictionary = @{}
    
    foreach($f in $files) {
        $in = Get-Content $f
        $in.Split([Environment]::NewLine) | ForEach-Object {
            $key,$value = $_.Split(':')
            $dictionary[$key] = $value
        }
        $dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] | Out-File -FilePath '.\results.csv' -Append
    }
    

    排序输出:

    $files      = ls "fil*.txt"
    $dictionary = @{}
    [System.Collections.Generic.List[String]]$list = @()
    
    foreach($f in $files) {
        $in = Get-Content $f
        $in.Split([Environment]::NewLine) | ForEach-Object {
            $key,$value = $_.Split(':')
            $dictionary[$key] = $value
        }
        [void]$list.Add( $dictionary['Ad Id'] + ', ' + $dictionary['Ad placement'] + ', ' + $dictionary['Ad Text'] )
    }
    [void]$list.Sort()
    $list | Out-File -FilePath '.\results.csv' -Append
    
        2
  •  1
  •   lit    8 年前

    下面是一种构建您所讨论的数组的方法。我认为这不是解决这个问题的最佳方法。这与结果的顺序无关,也不创建.csv文件。

    $FILES = Get-ChildItem -File -Filter "*.txt"
    
    $array1 = $()
    
    foreach($f in $FILES) {
        Get-Content -Path $f |
            Select-String -Pattern "Ad Id.*" |
            ForEach-Object { $array1 += @($_.Matches.Value) }
    }
    
    $FILES.Count
    
    $array1.Count
    $array1
    
        3
  •  1
  •   LotPings    8 年前

    另一种稍有不同的方法。

    • regex解析$line并创建一个名称在冒号前面的变量(不带 Ad )珍惜背后的一切
    • 在每个处理过的文件之后,vars将作为自定义对象输出。

    $Data = ForEach ($File in (Get-ChildItem File*.txt)){
        $Id,$Text,$Placement="","",""
        ForEach ($Line in (Get-Content $File)){
            If ($Line -Match "AD (?<Label>.*?): (?<Value>.*)"){
                Set-Variable -Name "$($Matches.Label)" -Value $Matches.Value
            }
        }
        [PSCustomObject]@{ID        = $Id
                          Placement = $placement
                          Text      = $Text}
    }
    $Data
    $Data | Export-CSv ".\Result.csv" -NoTypeInformation
    

    样品输出:

    ID   Placement Text
    --   --------- ----
    xxxx spaceship blah blah
    yyyy zoo       blah blah
    
    推荐文章