删除 PowerShell 中特定行之后的所有行

Delete all lines after a specific line in PowerShell

我有这个 .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 }}

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

您可能需要做一些每次都需要测试的事情。我会像这样使用 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)

but break ends my program

原因是管道中的break不退出管道,它退出任何封闭的循环foreachfordowhile 语句),如果有 none,则脚本为一个整体退出。

但是,您可以简单地修改您的方法以使用 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' , 如果需要):

# 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]
}
  • (?sm) 设置内联正则表达式选项 s(使 . 也匹配换行符)和 m(使 ^$ 匹配各行的结尾和开头)

  • .* 匹配任何字符序列,包括 none

  • ^{0}$ 匹配转义停止线(通过 [regex]::Escape(), to ensure that it is matched literally) in full, where {0} is the placeholder for the escaped line substituted by -f, the string formatting operator

  • 如果-matchregular-expression matching operator, succeeds, it populates the automatic $Matches variable,其0条目包含正则表达式完全匹配的内容。