-
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:
|
Beta Was this translation helpful? Give feedback.
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
- line4 Note that to append we use the $ dasel put string -f test.yaml -o - "list.[1]" "line4"
list:
- line1
- line4
- line3 Here'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_filename E.g. $ cat test.yaml
---
list:
- line1
- line2
- line3
$ cat test.out.yaml
---
list:
- line1
- line2
- line3
- line4
- line5
- line6 If you'd rather have the list rewritten then you can update |
Beta Was this translation helpful? Give feedback.
-
Exactly the kind of info I was looking for. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
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.sh
that will append the bash array items totest.yaml
and write the results totest.out.yaml
: