【问题标题】:Delete all lines after a specific line in PowerShell删除 PowerShell 中特定行之后的所有行
【发布时间】:2020-02-18 19:49:45
【问题描述】:

我有这个.txt 文件

ROTHSCHILD  = 81;        // Fondation Adolphe de Rothschild          ,                          2019
ONCOPOLE    = 82;        // Oncopole - Toulouse                      ,                          2019
GHRMSA      = 83;        // GHR  Mulhouse Sud-Alsace                 ,                          2019
CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019
CAEN        = 85;        // CHU  de Caen                             ,                          2019
MONTELIMAR  = 86;        //                                          ,                          2019
PUYENVELAY  = 87;        //                                          ,                          2019

我想删除

之后的所有行
CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019

我该怎么做?

我试过了:

get-content txt.txt | % {$_ ; if($_ -eq "CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019") {break }}

但是中断了我的程序,我该怎么做?

【问题讨论】:

    标签: powershell text-extraction


    【解决方案1】:

    您可能需要每次都做一些测试。我会像这样使用Do..Until 循环:

    $txtData = get-content txt.txt
    $stophere = 'CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019'
    $ctr = 0
    Do
    {
        $txtData[$ctr++] #Prints data line and increments counter
    } Until ($txtData[$ctr] -eq $stophere)
    

    【讨论】:

      【解决方案2】:

      但是break 结束了我的程序

      原因是管道中的break不会退出管道,而是退出任何封闭的循环foreachfordowhile 语句),如果没有,则整个脚本退出。

      但是,您可以简单地修改您的方法以使用foreach 循环,其中break 按预期工作:

      # The last line to include in the output.
      $stopLine = 'CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019'
      
      # Output all lines up to and including the stop line.
      # Simply assign to a variable to capture all output lines:
      #   $linesOfInterest = foreach ($line ...) { ... }
      # Or wrap in $(...) to send to a file.
      #   $(foreach ($line ...) { ... }) | Set-Content ...
      foreach ($line in Get-Content file.txt) {
        $line 
        if($line -eq $stopLine) {break }
      }
      

      或者,使用基于正则表达式的解决方案,将所有感兴趣的行捕获为单个多行字符串(如果需要,您可以使用-split '\r?\n' 将其拆分为行): p>

      # The last line to include in the output.
      $stopLine = 'CHDN        = 84;        // CH   du Nord - Luxembourg                ,                          2019'
      
      # Use the -match operator with a regex that that captures everything
      # from the beginning of the the file through the stop line, inclusively.
      if ((Get-Content -Raw file.txt) -match '(?sm).*^{0}$' -f [regex]::Escape($stopLine)) {
        # Output what was captured.
        # Append -split '\r?\n' to split into an array of lines.
        $Matches[0]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        相关资源
        最近更新 更多