I have a text file with the below text on multiple places:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: harbor-exporter
labels:
release: mgmt
I want to have:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: harbor-exporter
namespace: test
labels:
release: mgmt
Because this text is present on multiple places in the file, I do need to search for: metadata:\n name:harbor-exporter and append the line namespace: test. Next to that, the metadata: should be present at the beginning of the line. What is the best way of achieving this with sed?
I have tried:
cat harbor-exporter.yaml | sed ':a;N;$!ba;s|metadata:\n name: harbor-exporter|NewString|g'
But I do need the word metadata: at the start of a line. So I put a ^ in front of metadata: and that failing.
Command which is failing:
cat harbor-exporter.yaml | sed ':a;N;$!ba;s|^metadata:\n name: harbor-exporter|NewString|g'
Thanks