简体   繁体   English

使用Powershell删除文本文件中的多行

[英]Delete multiple lines in a text file using Powershell

I have a text file like below: 我有一个如下文本文件:

[Option]
DuplicateUncoveredDiff = 1
KeptPatternMaxCount = 7

[6107]
CtrlFlag = 10
Version =1532

[6900]
CtrlFlag = 10
Version =1532

...and some more text here

I am trying to find a way to delete multiple lines under a group. 我正在尝试找到一种删除组下多行的方法。 When I say group, I am referring to the strings inside [ ] brackets and whatever is under it. 当我说group时,我指的是[]括号内的字符串及其下的所有内容。

For example, this is Group 1: 例如,这是第1组:

[Option]
DuplicateUncoveredDiff = 1
KeptPatternMaxCount = 7

Group 2: 第2组:

[6107]
CtrlFlag = 10
Version =1532

Is there a way to remove the lines [Group 2]? 有没有办法删除行[Group 2]? Take note that line number or range cannot be used as this text file is not static. 请注意,不能使用行号或范围,因为此文本文件不是静态的。

So what I did, I parsed all the strings, replaced newline with "@" and looked for the value 6107 using -like operator, copied the content to another text file. 因此,我做了所有的字符串,将换行符替换为“ @”,然后使用-like运算符查找值6107 ,然后将内容复制到另一个文本文件中。 But what I would like to achieve is instead of selecting the group, I would like to deselect it - selecting all other lines except 6107 group. 但是我想实现的是代替选择组,而是取消选择组-选择除6107组之外的所有其他行。 I tried playing with the replace, putting the wildcard after 6107] and before the first occurrence of [ but it deleted everything. 我尝试使用替换,将通配符放在6107]之后和[第一次出现之前],但它删除了所有内容。

$content = Get-Content $filePath\$fileName -Raw
$content -replace "`n|`r", "@" | Set-Content $localPath\$newFile

$newContent = Get-Content $localPath\$newFile

if($newContent -like "*6107*")
{
$newContent -replace ".*6107]","" -replace "\[.*","" | Set-Content 
"$folderPath\6107.txt"
}

Please help. 请帮忙。 TIA! TIA!

Since you seem to have trouble with regex, let's try if we can do it without. 由于您似乎在使用正则表达式时遇到问题,因此请尝试是否可以不使用它。 (There are many threads here on what you did wrong, so I don't want to go into this, plus it's always good to be keep in mind that most of the time there is more than one way to do something.) (这里有很多关于您做错了什么的线程,因此,我不想赘述,而且记住大多数时间,做某事的方法不止一种,这总是一件好事。)

You have some lines of text, and for each of them you want to decide if you want to keep it or not, like this: 您有一些文本行,对于每个文本行,您都需要确定是否要保留它,例如:

keep   [Option]
keep   DuplicateUncoveredDiff = 1
keep   KeptPatternMaxCount = 7
keep   
       [6107]
       CtrlFlag = 10
       Version =1532

keep   [6900]
keep   CtrlFlag = 10
keep   Version =1532
keep   
keep   ...and some more text here

So assuming we go through the text line-wise, what we need is a "keep" flag that is $true by default and flips to $false when "[6107]" is encountered, and back to $true again when the next "[" comes up. 因此,假如我们通过的文本行的角度来看,我们需要的是一个“守”标志,该标志是$true默认并翻转至$false"[6107]"遇到了,回$true时再下一个"["出现。 This flag can then be used to filter the lines. 然后可以使用此标志来过滤行。

We can use the Where-Object cmdlet to keep track of the flag and filter the lines: 我们可以使用Where-Object cmdlet来跟踪标志并过滤行:

$keep = $true
Get-Content yourtextfile.txt | where { 
  if ( $_.StartsWith("[6107]") ) {
    $keep = $false
  } elseif ( -not $keep -and $_.StartsWith("[") ) {
    $keep = $true
  }
  $keep
}

Get-Content splits the text file into lines for you already, so we can pipe them right to Where-Object . Get-Content已经为您将文本文件分割成几行,因此我们可以将它们直接传递到Where-Object

Note how $keep flips to $false and stays $false until the next line starting with "[" . 注意$keep如何翻转到$false并保持$false直到下一行以"["开头。

You can pipe the result of that right to Set-Content to write it to a new file. 您可以将该权限的结果传递给Set-Content以将其写入新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM