如何使用shell脚本解析YAML文件?

f4t66c6m  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(240)

我有一个YAML文件,其中也有列表。
YAML文件-

configuration:
  account: account1
  warehouse: warehouse1
  database: database1
  object_type:
    schema: schema1
    functions: funtion1
    tables:
      - table: table1
        sql_file_loc: some_path/some_file.sql
      - table: table2
        sql_file_loc: some_path/some_file.sql

我希望将键对值存储到shell变量中,然后循环访问它。例如,account/warehouse/database的值应该存储到变量中,以便以后使用。另外,tables(table 1和table 2)和sql_file_loc的值应该存储到shell变量中,以便进行如下循环-

for i in $table ;do 
    echo $i
done

我试过下面的代码-

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);
      }
   }'
}

这是我得到的输出-

configuration_account="account_name"
configuration_warehouse="warehouse_name"
configuration_database="database_name"
configuration_object_type_schema="schema1"
configuration_object_type_functions="funtion1"
configuration_object_type_tables__sql_file_loc="some_path/some_file.sql"
configuration_object_type_tables__sql_file_loc="some_path/some_file.sql"

它不会打印-配置对象类型表表=“表1”和配置对象类型表表=“表2”
同样,对于列表,它打印两个下划线(__),这与其他对象不同。我想循环存储在configuration_object_type_tables__table和configuration_object_type_tables__sql_file_loc中的值。
任何帮助都将不胜感激!

kcrjzv8t

kcrjzv8t1#

考虑使用YAML处理器mikefarah/yq。它是一个单行程序:

yq e '.. | select(type == "!!str") | (path | join("_")) + "=\"" + . + "\""' "$INPUT"

输出

configuration_account="account1"
configuration_warehouse="warehouse1"
configuration_database="database1"
configuration_object_type_schema="schema1"
configuration_object_type_functions="funtion1"
configuration_object_type_tables_0_table="table1"
configuration_object_type_tables_0_sql_file_loc="some_path/some_file.sql"
configuration_object_type_tables_1_table="table2"
configuration_object_type_tables_1_sql_file_loc="some_path/some_file.sql"

另外,请看一下yq的这个很酷的内置特性:

yq e -o props "$INPUT"

输出

configuration.account = account1
configuration.warehouse = warehouse1
configuration.database = database1
configuration.object_type.schema = schema1
configuration.object_type.functions = funtion1
configuration.object_type.tables.0.table = table1
configuration.object_type.tables.0.sql_file_loc = some_path/some_file.sql
configuration.object_type.tables.1.table = table2
configuration.object_type.tables.1.sql_file_loc = some_path/some_file.sql
ltskdhd1

ltskdhd12#

我建议你试试jpseng提到的yq yaml processor
关于你在这里的代码,正则表达式是不匹配的“-表”模式,由于“-“前缀。

相关问题