json - How to update YAML files using shell pipes? -
i looking simple unix approach saving data obtained using shell scripts yaml files, example want save list of installed packages (rpms, pypi) system in single yaml file using shell pipes, like:
rpm -qa | sort | some-yaml-tool manifest.yaml system.packages
the expected results manifest.yaml file like:
system: packages: - xz-5.2.2-2.fc24.x86_64 - xz-devel-5.2.2-2.fc24.x86_64 while prefer yaml, not refuse json compatible solution. interested in finding way tools available on distributions, not esoteric need installed manually or non-official yum/deb repositories.
if tool smart, should able create file , internal path when exist.
please note had remove jq tool list because not allow in-place editing.
inserting package names yaml file
you don't need in-place editing support intended use case @ all. noting yaml superset of json, , output jq valid yaml:
rpm -qa | sort | jq -rn '{"system": {"packages": [inputs]}}' will generate output file of form:
{ "system": { "packages": [ "bar", "baz", "foo" ] } } ...in single pass. make more idiomatic yaml, can parse through tiny python shim using pyyaml library:
yaml_format() { python -c 'import sys, yaml; sys.stdout.write(yaml.dump(yaml.load(sys.stdin), default_flow_style=false))' } rpm -qa | sort | jq -rn '{"system": {"packages": [inputs]}}' | yaml_format ...will generate content of form:
system: packages: - bar - baz - foo updating json file in-place
let's have json file content other than manifest want preserve. in case, question asked explicitly becomes relevant. safe (albeit gnu-only) implementation following:
atomic_update() { # usage: atomic_update filename command arg1 arg2 ... local filename tempfile retval=0 filename=$1; shift || return tempfile=$(mktemp -t -- "$filename.xxxxxx") || return if "$@" <"$filename" >"$tempfile"; # make best-effort attempt preserve permissions chown --reference="$filename" -- "$tempfile" &>/dev/null ||: chmod --reference="$filename" -- "$tempfile" &>/dev/null ||: mv -- "$tempfile" "$filename" else retval=$? rm -f -- "$tempfile" fi return "$retval" } rpm -qa | sort | atomic_update manifest.yml jq -rn '.system.packages = [inputs]'
Comments
Post a Comment