Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacement of value within multiple document YAML file removes first document #1056

Closed
ghost opened this issue Dec 29, 2021 · 4 comments
Closed
Labels

Comments

@ghost
Copy link

ghost commented Dec 29, 2021

Describe the bug
When I try to replace a value within the second document of a YAML file containing multiple documents the first documents is removed, where I expect that the files stays intact. Hopefully there is an other way of doing this or a workaround. I saw one related issue, but that one is already closed: #351

Version of yq: 4.X.X
yq (https://github.com/mikefarah/yq/) version 4.16.2

OS: Windows WSL

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

foo: hello
---
bar:
  - name: bla
    version: yahoo

Command
The command you ran:

yq eval 'select(documentIndex == 1) | .bar[0].version="33"' test.yaml -i

(I got this example from: https://mikefarah.gitbook.io/yq/upgrading-from-v3#multiple-documents)

Actual behavior
The new file:

bar:
  - name: bla
    version: "33"

Expected behavior

foo: hello
---
bar:
  - name: bla
    version: "33"
@ghost ghost added bug v4 labels Dec 29, 2021
@mikefarah
Copy link
Owner

You just need brackets around the LHS so that you are not first filtering and then updating: https://mikefarah.gitbook.io/yq/usage/tips-and-tricks#updating-deeply-selected-paths

@ghost
Copy link
Author

ghost commented Dec 30, 2021

You just need brackets around the LHS so that you are not first filtering and then updating: https://mikefarah.gitbook.io/yq/usage/tips-and-tricks#updating-deeply-selected-paths

How would you suggest to do it? I tried yq eval '.bar[].version = "33" | select(documentIndex == 1)' test.yaml -i but I got the same result.

@mikefarah
Copy link
Owner

Basically what you need to do is put brackets around the whole expression you want updated:

yq eval '(select(documentIndex == 1) | .bar[].version )  = "33"' test.yaml -i

With brackets, this finds the matches of ( select(documentIndex==1 ) | .bar[].version) and updates that.

Without the brackets, this first selects documentIndex 1 (and leaves all the others out) and then, separately, finds the matches of (.bar[].version) and updates that.

Make sense?

@ghost
Copy link
Author

ghost commented Dec 31, 2021

Aaah, now I get it! It worked, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant