jrz
jrz

Reputation: 1387

yq command to remove unnecessary fields

Given a yaml file, I am looking for a yq command that will output a more "clean" list with only the relevant key "name" and its value.

for example, an output of the following yaml:

services:
  - name: svc1
    location: asia
    timezone: india
  - name: svc2
    location: europe
    timezone: uk
    cities:
      - london
  - name: svc3
    location: usa
    timezone: east
    cities:
      - new-york
      - brooklyn

Should be:

services:
  - name: svc1
  - name: svc2
  - name: svc3

Upvotes: 0

Views: 158

Answers (1)

pmf
pmf

Reputation: 36088

Which implementation of yq are you using? In both, you can use the pick function to reduce an object to given keys, but their syntax is slightly different:

Using mikefarah/yq (Tested with version 4.35.1):

yq '.services |= map(pick(["name"]))'

Using kislyuk/yq (Tested with version 3.2.3):

yq -y '.services |= map(pick(.name))'

Output:

services:
  - name: svc1
  - name: svc2
  - name: svc3

Upvotes: 0

Related Questions