0

So, I have an output from a command (kustomize build) and I want to convert the --- in the output to ###. For example:

$ kustomize build
apiVersion: extensions/v1
kind: Ingress
metadata: 
  labels:
    app: hello-world
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  ports:
  - name: service
    port: 443
    targetPort: 8443
  selector:
    app: hello-world
  type: NodePort

and I want to change it to :

apiVersion: extensions/v1
kind: Ingress
metadata: 
  labels:
    app: hello-world
###
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  ports:
  - name: service
    port: 443
    targetPort: 8443
  selector:
    app: hello-world
  type: NodePort

I tried $ kustomize build | tr '\---' '#' but that replaces every instance of even a single '-' to '#'. I even tried tr '[-]{3}' '#' but even that didn't help. How can I go about doing this?

Saturnian
  • 1,686
  • 6
  • 39
  • 65

1 Answers1

2

tr only translates single characters (and removes them, if you specify that).

What you need to use is the "stream editor" sed which can replace groups of characters:

sed 's/---/###/g'
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 1
    Depending on whether `------` should be replaced by `######` or not, it may be relevant to use `sed 's/^---$/###/g'` – mouviciel Sep 03 '19 at 09:00
  • 1
    @mouviciel More importantly, the bare string `---` could occur in lots of contexts other than that of document marker. – chepner Sep 03 '19 at 11:55
  • (And even then, `^---$` could match a line in a multiline string. Simply put, `sed` isn't really up to the task of editing YAML in general.) – chepner Sep 03 '19 at 11:56
  • @chepner OP never specified that the input is YAML ;-) – Alfe Sep 03 '19 at 13:27