Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash yaml parser library

function parse_yaml {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '34')
   sed -ne "s|,$s]$s$|]|" 
        -e ":1;s|^($s)($w)$s:$s[$s(.*)$s,$s(.*)$s]|12: [3]
1  - 4|;t1" 
        -e "s|^($s)($w)$s:$s[$s(.*)$s]|12:
1  - 3|;p" $1 | 
   sed -ne "s|,$s}$s$|}|" 
        -e ":1;s|^($s)-$s{$s(.*)$s,$s($w)$s:$s(.*)$s}|1- {2}
1  3: 4|;t1" 
        -e    "s|^($s)-$s{$s(.*)$s}|1-
1  2|;p" | 
   sed -ne "s|^($s):|1|" 
        -e "s|^($s)-$s["'](.*)["']$s$|1$fs$fs2|p" 
        -e "s|^($s)-$s(.*)$s$|1$fs$fs2|p" 
        -e "s|^($s)($w)$s:$s["'](.*)["']$s$|1$fs2$fs3|p" 
        -e "s|^($s)($w)$s:$s(.*)$s$|1$fs2$fs3|p" | 
   awk -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
      if(length($2)== 0){  vname[indent]= ++idx[indent] };
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) { vn=(vn)(vname[i])("_")}
         printf("%s%s%s="%s"
", "'$prefix'",vn, vname[indent], $3);
      }
   }'
}


>>> eval $(parse_yaml test.yaml "")

# Input test.yaml
global:
  input:
    - "main.c"
    - "main.h"
  flags: [ "-O3", "-fpic" ]
  sample_input:
    -  { property1: value, property2: "value2" }
    -  { property1: "value3", property2: 'value 4' }
    
# Output for parse_yaml
global_input_1="main.c"
global_input_2="main.h"
global_flags_1="-O3"
global_flags_2="-fpic"
global_sample_input_1_property1="value"
global_sample_input_1_property2="value2"
global_sample_input_2_property1="value3"
global_sample_input_2_property2="value 4"

Comment

PREVIOUS NEXT
Code Example
Shell :: regex for url in bash 
Shell :: go to a tag in git 
Shell :: add new domain again with certbot 
Shell :: ubuntu mount samba dir 
Shell :: vscode installation ubuntu 
Shell :: remove branch local git 
Shell :: convert audio to mp3 with ffmpeg 
Shell :: pip upgrade version 
Shell :: find file location linux 
Shell :: doe snot appear to be a git repository 
Shell :: kubectl connect to node bash 
Shell :: run bat file 
Shell :: sudo pkg instatll lando-stable.deb 
Shell :: git branch change remote 
Shell :: how to git clone into a directory 
Shell :: how to create a github account 
Shell :: gitignore generator 
Shell :: centos 8 force update ip address 
Shell :: start tomcat 9 as a service windows 
Shell :: switch user ubuntu 
Shell :: how to edit crontab macos 
Shell :: create branch git 
Shell :: compare repositories github 
Shell :: how to merge git 
Shell :: install wp cli wp org 
Shell :: install pug angular 
Shell :: linux kill all process of a user 
Shell :: debian change default kernel 
Shell :: install spotify on ubuntu 
Shell :: linux change permissions recursive only directories 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =