|
I have an array stored in bash variable containing data, eg: Now I want to append those lines to a single subkey, like so: Can't figure out how to do that. The closest I got was this: This produces the following: |
Answered by
TomWright
May 31, 2022
Replies: 2 comments 1 reply
|
The dasel command you're looking for is: $ dasel put string -f test.yaml -o - "list.[]" "line4"
list:
- line1
- line2
- line3
- line4Note that to append we use the $ dasel put string -f test.yaml -o - "list.[1]" "line4"
list:
- line1
- line4
- line3Here's an example #!/bin/bash
filename=test.yaml
out_filename=test.out.yaml
# new items to be added to the list
array=(line4 line5 line6)
# copy filename contents to out_filename.
cp $filename $out_filename
for ((i=0;i<${#array[*]};i++))
do
{
dasel put string -f "$out_filename" "list.[]" "${array[$i]}"
}
done
echo -e "---\n$(cat $out_filename)" > $out_filenameE.g. $ cat test.yaml
---
list:
- line1
- line2
- line3
$ cat test.out.yaml
---
list:
- line1
- line2
- line3
- line4
- line5
- line6If you'd rather have the list rewritten then you can update |
1 reply
Answer selected by
paradajz
|
Exactly the kind of info I was looking for. Thanks a lot! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The dasel command you're looking for is:
Note that to append we use the
[]selector rather than[$i]. Using[$i]you will be overwriting the value at that index.Here's an example
run.shthat will append the bash array items totest.yamland write the results totest.out.yaml: