parse_yaml.sh 543 B

1234567891011121314151617
  1. #!/bin/sh
  2. parse_yaml() {
  3. local prefix=$2
  4. local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
  5. sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
  6. -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
  7. awk -F$fs '{
  8. indent = length($1)/2;
  9. vname[indent] = $2;
  10. for (i in vname) {if (i > indent) {delete vname[i]}}
  11. if (length($3) > 0) {
  12. vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
  13. printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
  14. }
  15. }'
  16. }