Sorry didn't see all the attention here, @tony-sol you can do it like this:

yq '.[] |= pick(["name", "status"])' data1.yaml

Explanation:

  • You want to pick particular fields from each map in the array
  • .[] |= will match each element in the array, and update that element w.r.t itself
  • pick([]) will take a given map and pick fields (omitting the rest)

See https://mikefarah.gitbook.io/yq/operators/pick for details.

The different with doing .[] | (.name, .status) is that will match all elements and just pulling out just those values.

🌐
yq
mikefarah.gitbook.io › yq › operators › select
Select | yq - GitBook
Select multiple items in a map and update · Copy · - go-kart - goat - dog · Copy · yq '.[] | select(. == "go*")' sample.yml · Copy · go-kart goat · Copy · - ago - go - meow - going · Copy · yq '.[] | select(. == "*go*")' sample.yml · Copy · ago go going ·
Discussions

How do I select multiple keys for output?
This seems like a basic question but I can't find examples anywhere in the documentation. If I have input like - alpha: 1 beta: foo gamma: 0.3 delta: bar - alpha: 2 beta: baz gamma: 1.23 delta: buz... More on github.com
🌐 github.com
14
October 29, 2019
jq - yq: change multiple values selected by regex - Stack Overflow
I want to change my docker-compose file service images via yq tool. The problem is that I have several services which have the same name prefix and I need to select their image fields and update th... More on stackoverflow.com
🌐 stackoverflow.com
Extracting certain fields to manipulate them individually and combine into line by line output
Given the following YAML: - name: Bob Blob start_date: 2022-11-05 slack: id: 0123 username: BOB hobbies: [] - name: Jane Doe start_date: 2022-12-12 slack: id: 1234 username: Jane My desired output ... More on github.com
🌐 github.com
1
1
conditional statements - How to select all YAML keys based on a subkey value using yq? - Stack Overflow
The trick is to convert the object ... the selection to the array elements and then convert it back into an object. The filter with_entries does these conversions under the hood. It is a shorthand for to_entries | map(foo) | from_entries (see jq manual). The call is slightly different depending on the yq implementation ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › mikefarah › yq › issues › 287
How do I select multiple keys for output? · Issue #287 · mikefarah/yq
October 29, 2019 - And I want to select only the beta and delta keys, so that I get the following output:
Author   ronniegane
🌐
yq
mikefarah.gitbook.io › yq › recipes
Recipes | yq - GitBook
Multiple or complex updates to items in an array · Explanation: Sort an array by a field · Explanation: Filter, flatten, sort and unique · Explanation: Export as environment variables (script), or any custom format · Explanation: Custom format with nested data · Explanation: Copy · - name: Foo numBuckets: 0 - name: Bar numBuckets: 0 · Copy · yq '(.[] | select(.name == "Foo") | .numBuckets) |= .
🌐
HackingNote
hackingnote.com › en › cheatsheets › jq
Cheatsheet - jq
Multiple fields: # from top level $ cat file.json | jq '. | {field1, field2}' # from sub level $ cat file.json | jq '.results[] | {id, name}' Extract as text values instead of JSON (getting rid of quotes), use -r: $ cat file.json | jq -r '.a.b[0].c' Slice an array · $ echo '["a", "b", "c", "d"]' | jq '.[1:3]' # get "name" of id 1 $ cat file.json | jq '.results[] | select(.id == "1") | {name}' # get result with multiple filters $ cat file.json | jq '.results[] | select((.id == "1") and (.name="a"))' # filter with substrings $ cat file.json | jq '.results[] | select(.name | contains("ab"))' # filter with regex $ cat file.json | jq '.results[] | select(.name | test("Joe\s+Smith"))' Given a list (items[]), find the item according to .spec.foo, change the value of .spec.bar to baz: cat file.json | jq '(.items[] | select(.spec.foo == "key")).spec.bar |= "baz"' Selectors start with ..
Find elsewhere
🌐
MetaCPAN
metacpan.org › dist › ETL-Yertl › view › bin › yq
yq - Filter YAML through a command-line program - metacpan.org
# INPUT foo: bar baz: fuzz $ yq 'if .foo eq bar then .baz else .foo' fuzz $ yq 'if .foo eq buzz then .baz else .foo' bar $ yq 'if .foo then .baz' fuzz $ yq 'if .herp then .derp else .' foo: bar baz: fuzz · The else FALSE_FILTER is optional and defaults to returning undefined. Combinators combine multiple expressions to yield one or more documents in the output stream.
🌐
Stack Overflow
stackoverflow.com › tags › yq › hot
Hottest 'yq' Answers - Stack Overflow
Using mikefarah/yq (aka Go yq), you could do something like below. Using del for deleting an entry and with_entries for selecting known fields yq '.items[] |= (del(.status) | .metadata |= with_entries(...
🌐
HackingNote
hackingnote.com › en › cheatsheets › yq
Cheatsheet - yq
Delete multiple fields: $ cat obj_backup.yaml | yq 'del(.status, .metadata.creationTimestamp, .metadata.resourceVersion, .metadata.uid, .metadata.generation)' > obj.yaml · Select from a list, by name: $ yq '.[] | select(.name == "Foo")' example.yml · Increment numBuckets: $ yq '(.[] | select(.name == "Foo") | .numBuckets) |= .
🌐
Medium
baturorkun.medium.com › learning-to-manipulate-yaml-on-the-command-line-with-yq-531268d8805d
Learning to Manipulate YAML on the Command Line with YQ | by Batur Orkun | Medium
October 31, 2023 - Maybe you need multiple pieces of information. yq e '"Total Services: " + (.services | length), (.services | keys)[]' services.yaml ... I only need image names that have ports. yq eval '.services | with_entries(select(.value.ports != null)) | .[] | .image' services.yaml
🌐
cheat.sh
cheat.sh › yq
cheat.sh/yq
For version 4, use 'yq_v4' # Read spec.template node from example.yml yq r example.yml spec.template # Read from stdin cat sample.yaml | yq r - b.c # Print the path yq r --printMode p "a.thing*.*" # Print the path and value yq r --printMode pv "a.thing*.*" # Print the length of a list yq r sample.yml --length animals # Read with conditions yq r sample.yml spec.metadata[name==myapp] # Collect results into an array yq r sample.yaml --collect a.*.animal # Read from the 2nd document yq r -d1 sample.yaml b.c # Validate a document
🌐
GitHub
github.com › mikefarah › yq › issues › 686
How do I select multiple keys for output? · Issue #686 · mikefarah/yq
January 22, 2021 - Issue #287 was closed as fixed without giving an example (@SteveH-US) of the syntax needed to select multiple keys. Input: - alpha: 1 beta: foo gamma: 0.3 delta: bar - alpha: 2 beta: baz gamma: 1.23 delta: buzz And I want to select only ...
Author   holmesb
🌐
DevPress
devpress.csdn.net › cicd › 62eb508a20df032da732b005.html
yq: Mastering YAML Processing in Command Line_devops_weixin_0010034-CI/CD
August 4, 2022 - # yq eval '.user.orders += 2845234' user.yaml ... orders: - 4356436 - 4345753 - 2345234 - 2845234 · Another handy one is update operator (=), which (surprise, surprise) updates some field.
🌐
Nhanvietluanvan
nhanvietluanvan.com › trang chủ › efficiently selecting multiple fields with jquery: a comprehensive guide
Efficiently Selecting Multiple Fields With Jquery: A Comprehensive Guide
July 7, 2023 - Benefits of YQ Select Multiple Fields: 1. Efficient Data Extraction: YQ Select Multiple Fields allows users to extract only the necessary data from a dataset, eliminating the need to retrieve unnecessary columns.
🌐
GitHub
github.com › mikefarah › yq › issues › 501
yq read multi path expression · Issue #501 · mikefarah/yq
July 22, 2020 - Is your feature request related to a problem? Please describe. I want to read multiple values from one file with only one command If we have data1.yml like: a: b c: d e: f f: g And we run a command: yq read data1.yml 'a' 'e' Or yq read d...
Author   abourree
🌐
Baeldung
baeldung.com › home › files › file editing › processing yaml content with yq
Processing YAML Content With yq | Baeldung on Linux
March 18, 2024 - $ yq personal_data.yaml name: "My name" surname: "Foo" street_address: city: "My city" street: "My street" number: 1 · We should use the path in the YAML file to retrieve the property’s value. First, let’s print the name property in personal_data.yaml: ... We can use the select operator to find a particular value.