Home > database >  Powershell Replace multiple lines
Powershell Replace multiple lines

Time:08-11

I need to remove the following block in multiple files

resources:
  limits:
    memory: 300Mi
  requests:
    memory: 128Mi

I'm trying with the following code with no success:

Get-ChildItem '..\.\Helm-Charts\containerizedcomponents\values.yaml' -Recurse | ForEach {
     (Get-Content $_) | ForEach  {$_ -Replace 'resources:
  limits:
    memory: 300Mi
  requests:
    memory: 128Mi', ''} | Set-Content $_
}

How can i do this?

CodePudding user response:

Here's an example. But watch the details. I'm assuming windows text with \r\n line endings. I would backup the files first. Command line and scripts may work differently; take out the \r in a script. This is very fussy.

"one`r
two`r
three`r
four`r
" | set-content file.txt -nonewline


(get-content -raw file.txt) -replace 'two\r
three\r
' | set-content -NoNewline file2.txt


get-content file2.txt

one
four

  • Related