Is it possible to modify a yml file via shell script?

I wrote https://github.com/kislyuk/yq, a wrapper around https://stedolan.github.io/jq/, to address this use case.


I wrote yaml_cli (https://github.com/Gallore/yaml_cli) to do exactly what you need. It's based on python. This would be the syntax for your example:

yaml_cli \
  -f docker-compose.yml \                # read from and save to file
  --list-append \                        # flag to append to lists instead of replacing existing values
  -s nginx:links newthing \              # add a value of type string; here you need --list-append
  -s newthing:container_name foo \       # key 'newthing' is created automatically
  -s newthing:image 'newthing:1.2.3' \   #
  -s newthing:restart always \           #
  -s newthing:hostname 'example.com'     #

Feedback about yaml_cli is appreciated.


There are a number of yaml libraries for Perl, Python etc. if it's ok to do it not directly from a shell script, but use another language.

Another option is to install a command-line yaml processor, and call it from your shell script.