LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2021, 10:00 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Unable to update K8s manifest file by sed


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.yaml
Error 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
 
Old 02-08-2021, 04:13 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
First, there are tools that are much better than sed suited for manipulating the structured text data like YAML. E.g. see this list.

Among others, there are two different, but similar tools, both named yq, which are essentially the same to YAML what jq is to JSON: yq written in Python by Andrey Kislyuk, and yq written in Go by Mike Farah.

This is how I would do it with the Python yq:
Code:
yq -Yi \
 'del(.metadata)|
  del(..|.uid?,.selfLink?,.resourceVersion?,.creationTimestamp?)' \
  128-res-test-black-dev4-serviceaccounts.yaml
While the Python yq is just a wrapper around jq, the Go yq is a rewrite that currently supports only a subset of jq commands. So for it, the expression above should be rewritten slightly more verbose:
Code:
yq -i eval \
 'del(.metadata)|
  del(.items[].metadata|(.uid,.selfLink,.resourceVersion,.creationTimestamp))' \
  128-res-test-black-dev4-serviceaccounts.yaml
Quote:
Originally Posted by sysmicuser View Post
Code:
sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /$ d' 128-res-test-black-dev4-serviceaccounts.yaml
You cannot combine regex with numeric addresses for one command in sed (unless it's a range). Rewrite it using two d commands:
Code:
sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /d;$d' 128-res-test-black-dev4-serviceaccounts.yaml
And using extended regular expressions this could be written more compactly:
Code:
sed -ri '/(uid|selfLink|resourceVersion|creationTimestamp): /d;$d' 128-res-test-black-dev4-serviceaccounts.yaml

Last edited by shruggy; 02-08-2021 at 05:48 AM.
 
Old 02-12-2021, 01:26 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Thank you Shruggy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
nvidia fx5200 problems on asus k8s-mx MoBo kinematic Linux - Hardware 1 10-17-2006 12:06 PM
K8S-LA (pavilion a1203w) SiS 760/964 i2c sensor question (Fedora Core 5) NeoKaiserSigma Linux - Hardware 0 07-31-2006 12:40 AM
Asus K8S-MX & Fedora core 4 lord_zoo Linux - Hardware 14 02-26-2006 11:39 AM
Onboard LAN with a Asus K8S-MX Pimm MEPIS 12 02-01-2006 03:41 PM
Cant install Linux on SATA HDD Asus k8S MX combo coolmohitz Linux - Hardware 4 09-02-2005 09:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration