【问题标题】:Insert Content into Text File in Powershell在 Powershell 中将内容插入文本文件
【发布时间】:2009-12-09 17:49:09
【问题描述】:

我想在 Powershell 的文本文件中间添加内容。我正在搜索特定模式,然后在其后添加内容。请注意,它位于文件的中间。

我目前拥有的是:

 (Get-Content ( $fileName )) | 
      Foreach-Object { 
           if($_ -match "pattern")
           {
                #Add Lines after the selected pattern
                $_ += "`nText To Add"
           }
      }
  } | Set-Content( $fileName )

但是,这不起作用。我假设是因为 $_ 是不可变的,还是因为 += 运算符没有正确修改它?

将文本附加到 $_ 的方法是什么,这将反映在以下 Set-Content 调用中?

【问题讨论】:

  • 原件的唯一问题是您没有输出任何内容。只需在 if(){} 块之后附加一个 $_ ...

标签: powershell


【解决方案1】:

只需输出额外的文本,例如

(Get-Content $fileName) | 
    Foreach-Object {
        $_ # send the current line to output
        if ($_ -match "pattern") 
        {
            #Add Lines after the selected pattern 
            "Text To Add"
        }
    } | Set-Content $fileName

您可能不需要额外的“n”,因为 PowerShell 会为您终止每个字符串。

【讨论】:

    【解决方案2】:

    这个怎么样:

    (gc $fileName) -replace "pattern", "$&`nText To Add" | sc $fileName
    

    我认为这是相当直截了当的。唯一不明显的是“$&”,它指的是“模式”匹配的内容。更多信息:http://www.regular-expressions.info/powershell.html

    【讨论】:

    • 好建议。不适用于我需要做的事情,但可以在更一般的情况下工作。
    • @Jeff,我相信它在功能上与您的相同。
    【解决方案3】:

    这个问题可以通过使用数组来解决。文本文件是一个字符串数组。每个元素都是一行文本。

    $FileName = "C:\temp\test.txt"
    $Patern = "<patern>" # the 2 lines will be added just after this pattern 
    $FileOriginal = Get-Content $FileName
    
    <# create empty Array and use it as a modified file... #>
    
    $FileModified = @() 
    
    Foreach ($Line in $FileOriginal)
    {    
        $FileModified += $Line
    
        if ($Line -match $patern) 
        {
            #Add Lines after the selected pattern 
            $FileModified += 'add text'
            $FileModified += 'add second line text'
        } 
    }
    Set-Content $fileName $FileModified
    

    【讨论】:

      【解决方案4】:

      我试图这样做,但使用 XAML 文本框。该主题为我提供了使其工作所需的起点。

      对于其他希望这样做的人:

      #Find and replace matched line in textbox
      $TextBox.Text = ($TextBox.Text) | 
      Foreach-Object {
          if ($_ -match "pattern")
          {
              #Replace matched line 
              $_ -replace "pattern", "Text To Add"
          }
      }
      

      【讨论】:

        【解决方案5】:
        (Get-Content $fileName) | Foreach-Object {
                if ($_ -match "pattern") 
                {
                    write-output $_" Text To Add"
                }
                else{
                    write-output $_
                    }
            } | Set-Content $fileName
        

        【讨论】:

        • 与接受的答案相比,这有什么好处?
        【解决方案6】:

        以下脚本用于在指定路径中的多个文件的模式之后插入文本

        $fileNames = Get-ChildItem "C:\Example" -Recurse |
        select -expand fullname
        
        foreach ($FileName in $filenames) 
        {
        $pattern = "pattern"
        
        [System.Collections.ArrayList]$file = Get-Content $FileName
        
        $insertafter = @()
        
        for ($i=0; $i -lt $file.count; $i++) {
          if ($file[$i] -match $pattern) {
            $insertafter += $i+1 #Record the position of the line after this one
          }
        }
        
        #Now loop the recorded array positions and insert the new text
        $insertafter | Sort-Object -Descending | ForEach-Object { 
        $file.insert($_, "text inserted after pattern") }
        
        
        Set-Content $FileName $file
        }
        

        【讨论】:

          【解决方案7】:

          txt,properties,pf etc 文件中文本的完全匹配解决方案。

          $FileName = "C:\Progress\OpenEdge\properties\fathom.properties"
          $Pattern = "[Fathom]"  
          $FileOriginal = Get-Content $FileName
          
          [String[]] $FileModified = @() 
          Foreach ($Line in $FileOriginal)
          {   
              $FileModified += $Line
              if ( $Line.Trim() -eq $Pattern ) 
              {
                  #Add Lines after the selected pattern 
                  $FileModified += "Autostart=true"
                  
              } 
          }
          Set-Content $fileName $FileModified
          

          【讨论】:

            猜你喜欢
            • 2021-11-12
            • 1970-01-01
            • 1970-01-01
            • 2021-02-06
            • 2013-04-27
            • 2017-11-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多