Skip to content

filter from array and append to another file #712

Answered by mikefarah
MarekGajdosik asked this question in Q&A
Discussion options

You must be logged in to vote
yq ea 'select(fi==1).a += (.a[] | select(fi==0 and .b == "21")) |  select(fi==1)' examples/data1.yaml examples/data2.yaml

Synopsis:

  • Use ea (or eval-all) so that both yaml files are read into memory and the expression is run once (by default yq runs the expression once per document to lower memory consumption).
  • Use select with the fileIndex fi operator to filter out nodes by the file index
  • We want to append to '.a' of file index 1 with the contents of .a in file index 0, where b==21
  • By default, all files are output, but in this case you only want to output fileIndex 1, so pipe it through a filter again

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@MarekGajdosik
Comment options

Answer selected by MarekGajdosik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants