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.
For example, this is Group 1:
[Option]
DuplicateUncoveredDiff = 1
KeptPatternMaxCount = 7
Group 2:
[6107]
CtrlFlag = 10
Version =1532
Is there a way to remove the lines [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. 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. I tried playing with the replace, putting the wildcard after 6107] and before the first occurrence of [ but it deleted everything.
$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!