Skip to content

Instantly share code, notes, and snippets.

@masukomi
Created June 4, 2015 21:11
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save masukomi/e587aa6fd4f042496871 to your computer and use it in GitHub Desktop.
Save masukomi/e587aa6fd4f042496871 to your computer and use it in GitHub Desktop.

Found here by Stephan Farestam

Here is a bash-only YAML parser that leverages sed and awk to parse simple yaml files:

function parse_yaml {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   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 |
   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%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
      }
   }'
}

It understands files such as:

## global definitions
global:
  debug: yes
  verbose: no
  debugging:
    detailed: no
    header: "debugging started"

## output
output:
   file: "yes"

Which, when parsed using:

parse_yaml sample.yml

will output:

global_debug="yes"
global_verbose="no"
global_debugging_detailed="no"
global_debugging_header="debugging started"
output_file="yes"

it also understands yaml files, generated by ruby which may include ruby symbols, like:

---
:global:
  :debug: 'yes'
  :verbose: 'no'
  :debugging:
    :detailed: 'no'
    :header: debugging started
  :output: 'yes'

and will output the same as in the previous example.

typical use within a script is:

eval $(parse_yaml sample.yml)

parse_yaml accepts a prefix argument so that imported settings all have a common prefix (which will reduce the risk of namespace collisions).

parse_yaml sample.yml "CONF_"

yields:

CONF_global_debug="yes"
CONF_global_verbose="no"
CONF_global_debugging_detailed="no"
CONF_global_debugging_header="debugging started"
CONF_output_file="yes"

Note that previous settings in a file can be referred to by later settings:

## global definitions
global:
  debug: yes
  verbose: no
  debugging:
    detailed: no
    header: "debugging started"

## output
output:
   debug: $global_debug

Another nice usage is to first parse a defaults file and then the user settings, which works since the latter settings overrides the first ones:

eval $(parse_yaml defaults.yml)
eval $(parse_yaml project.yml)
@VladSavitsky
Copy link

VladSavitsky commented Dec 27, 2022

Thanks for your work. I've created a function which gets list of variable name of desired level.
Could be used to loop variables when their their names are unknown and could be found only in YAML-file.

###############################################################################
# Usage: yaml_get_vname <file_path> [<level:0>] [<prefix>]
# Summary: Gets list of names of parent variables of given <level> in YAML-file.
# Help:
#   Params:
#     <file_path> Path to YAML-file.
#     [<level:|0|1|2...N>] Level of variable. Starts from 0.
#     [<prefix>] Prefix which will be added to variable names.
yaml_get_vname() {
  local filepath=$1;
  local level=${2:-0};
  local prefix=$3

  local s='[[:space:]]*';
  local w='[a-zA-Z0-9_]*';
  local fs=$(echo @|tr @ '\034');
  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"  $filepath |
  awk -F$fs '{
    indent = length($1)/2;
    if (indent == '$level' && length($3) == 0) {
      printf("%s%s\n", "'$prefix'", $2);
    }
  }'
}

@masukomi
Copy link
Author

awesome. Thanks @VladSavitsky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment