Skip to content

Instantly share code, notes, and snippets.

@subhojit777
Created October 7, 2015 15:46
Show Gist options
  • Save subhojit777/2df7dae5df9bce9ace95 to your computer and use it in GitHub Desktop.
Save subhojit777/2df7dae5df9bce9ace95 to your computer and use it in GitHub Desktop.
Parse YAML file and only fetch value from key-value pair
#!/bin/bash
# Thanks to http://stackoverflow.com/a/21189044/1233922
prefix=$2
s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|/usr/bin/tr @ '\034')
/usr/bin/sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
/usr/bin/awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("\%s\n", $3);
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment