Balaji Vignesh
Balaji Vignesh

Reputation: 456

How to select between multiple lines in power shell?

I am using Get-Content (gc) here. I need to delete a set of lines defined by the start and end marker. But for some reasons, the command that I use does not work. Do you know what I am missing here?

Command that I have tried:

powershell -Command ((gc test.txt) -replace '"/\#start.*\#end/gms"','')

test.txt:

line1
line2
#start
line3
line4
#end
line5

Expected output:

line1
line2
line5

Upvotes: 2

Views: 601

Answers (2)

iRon
iRon

Reputation: 23830

As answered by @TobyU, using the -Raw will easily resolve the issue but there is a downside to this quick solution:

PowerShell is extremely good in streaming objects, that's why Get-Content supplies a stream of lines in the first place. If you use the -Raw and/or brackets, you choke the output pipeline. This is not a big issue for a small list but could cause performance issues and/or memory issues when the list gets larger.

In respect of the PowerShell streaming pipeline you might consider to resolve it like this:

$On = $True; Get-Content -Path test.txt | Where {If ($_ -eq '#start') {$On = $False} ElseIf ($_ -eq '#end') {$On = $True} Else {$On}}

In this command, Where filters the #Start and #End as it has in both cases no output at all and passes the rest of the lines when $on is $True.

Upvotes: 2

TobyU
TobyU

Reputation: 3918

Get-Content reads the file into an Array of strings. Use Get-Content -Raw to read it as one string.

powershell -Command ((Get-Content -Path test.txt -Raw) -replace '(?smi)#start(.*)#end\r?\n','')

Upvotes: 3

Related Questions