0

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?

2
  • 2
    Instead of using dubious string replacements, I would use a yaml parser Commented Aug 10, 2022 at 15:00
  • 2
    get-content -raw to get more than one line, but I would make backup copies first. There's also taking \r and \n into account. Set-content also adds a newline by default. Commented Aug 10, 2022 at 15:16

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.