Remove all text after line with [info]

Viewed 145

I am trying to remove all lines of text after a single line of text "[info]" Here is an example:

Top=1266
[info]
name=tod
space=456
number=221,441,111,0
[version]
version=1

I only need the top, the other text will be replaced later on in the script. Here is all that I have tried

$Content -replace '\[Info\]*',''

Only removes the Info line and not anything past that. I have tried to loop, but I can't seem to find the line with a where object search.

What is a quick and easy way to remove all lines of code after a single line of set text?

1 Answers

To make the -replace operator treat it as one string, add (?s) to the pattern.

$Content -replace '(?s)\[Info\].*'

You also needed to match any character so .* works in this case. The second part is optional. Since you're replacing it with nothing you can simply omit it.

Read more about regular expression in powershell

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.1

and operators

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.1

Related