Unable to update K8s manifest file by sed
by sysmicuser from LinuxQuestions.org on (#5DWZ9)
I have the below YAML file as like below:
Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2019-03-20T23:17:42Z
name: default
namespace: dev4
resourceVersion: "80999"
selfLink: /api/v1/namespaces/dev4/serviceaccounts/default
uid: 5c6e0d09-4b66-11e9-b4e3-0a779a87bb40
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
creationTimestamp: 2020-04-21T05:46:25Z
name: pod-labeler
namespace: dev4
resourceVersion: "113455688"
selfLink: /api/v1/namespaces/dev4/serviceaccounts/pod-labeler
uid: 702dadda-8393-11ea-abd9-0a768ca51346
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
resourceVersion: ""
selfLink: ""I am using sed to upate the file to be applied to a cluster, my sed command is like below:
Code:sed -i \'/uid: \\|selfLink: \\|resourceVersion: \\|creationTimestamp: /d\' test1-serviceaccounts.yaml
However, I get the YAML file as like below:
Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: dev4
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
name: pod-labeler
namespace: dev4
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
How should I modify my sed expression to get rid of the last line `metadata:`
Note: It should delete the last line.
I did try the below sed command however, it produces an error:
Updated sed command:
Code: sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /$ d' 128-res-test-black-dev4-serviceaccounts.yamlError from executing the above command:
Code:sed: -e expression #1, char 60: unknown command: `$'The target file should look like this:
Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: dev4
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
name: pod-labeler
namespace: dev4
secrets:
- name: pod-labeler-token-6vgp7
kind: List


Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2019-03-20T23:17:42Z
name: default
namespace: dev4
resourceVersion: "80999"
selfLink: /api/v1/namespaces/dev4/serviceaccounts/default
uid: 5c6e0d09-4b66-11e9-b4e3-0a779a87bb40
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
creationTimestamp: 2020-04-21T05:46:25Z
name: pod-labeler
namespace: dev4
resourceVersion: "113455688"
selfLink: /api/v1/namespaces/dev4/serviceaccounts/pod-labeler
uid: 702dadda-8393-11ea-abd9-0a768ca51346
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
resourceVersion: ""
selfLink: ""I am using sed to upate the file to be applied to a cluster, my sed command is like below:
Code:sed -i \'/uid: \\|selfLink: \\|resourceVersion: \\|creationTimestamp: /d\' test1-serviceaccounts.yaml
However, I get the YAML file as like below:
Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: dev4
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
name: pod-labeler
namespace: dev4
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
How should I modify my sed expression to get rid of the last line `metadata:`
Note: It should delete the last line.
I did try the below sed command however, it produces an error:
Updated sed command:
Code: sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /$ d' 128-res-test-black-dev4-serviceaccounts.yamlError from executing the above command:
Code:sed: -e expression #1, char 60: unknown command: `$'The target file should look like this:
Code:apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: dev4
secrets:
- name: default-token-tl4dd
- apiVersion: v1
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
name: pod-labeler
namespace: dev4
secrets:
- name: pod-labeler-token-6vgp7
kind: List