首页 > 解决方案 > Unix连接嵌套/缩进的值列表?

问题描述

我正在尝试从带有缩进(2 个空格)的键值对列表中生成分层列表。修改原始内容

编辑:道歉。我最终粘贴了错误的输出。原始 yaml 文件就是这种格式。获得“描述”是我的次要目标:

schemas:
- name: exports
  tables:
  - name: sugar
    description: makes stuff sweet
    active_date: 2019-01-07 00:00:00
    columns:
    - name: color
      type: abcd
    - name: taste
      type: abcd
      description: xyz
      example: 21352352
    - name: structure
      type: abcd
      description: xyzasaa
      example: 10001
  - name: salt
    description: not that sweet.
      makes it salty.
    active_date: 2018-12-18 00:00:00
    columns:
    - name: strength
      type: abcdef
      description: easy to find
      example: 2018-03-03 12:30:00
    - name: color
      type: abcdeffa
      description: not sweet
      example: 21352352
    - name: quality
      type: abcd
      description: how much is needed
      example: 10001

最理想的输出将在下面,我试图生成一个 csv 并将 yaml 展平,每一行都带有所有父值的子元素:

sugar.color,abcd
sugar.taste,abcd,xyz
sugar.structure,abcd,xyzasaa
salt.strength,abcdef,"easy to find"
salt.color,abcdeffa,"not sweet"
salt.quality,abcd,"how much is needed"

但我不知道上述方法有多可行,所以至少要寻找:

sugar.color
sugar.taste
sugar.structure
salt.strength
salt.color
salt.quality

标签: bashunixawksed

解决方案


在任何 UNIX 机器上的任何 shell 中使用任何 awk:

$ cat tst.awk
BEGIN { OFS = "," }

match($0,/^ +- /) { indent = RLENGTH }

$1 == "-" {
    prt()
    if (indent == 4) {
        key = $NF
        subKey = ""
    }
    else if (indent == 6) {
        subKey = $NF
    }
    next
}

subKey != "" {
    data = substr($0,indent+1)

    if ( data ~ /^[^[:space:]]/ ) {
        # new data
        tag = data
        sub(/:.*/,"",tag)
        sub(/^[^:]+: */,"",data)
        f[tag] = data
    }
    else {
        # continuation of previous data
        sub(/^[[:space:]]*/,"",data)
        f[tag] = f[tag] " " data
    }
}

END { prt() }

function prt() {
    if ( "type" in f ) {
        print key "." subKey, f["type"], "\"" f["description"] "\""
    }
    delete f
}

$ awk -f tst.awk file
sugar.color,abcd,""
sugar.taste,abcd,"xyz"
sugar.structure,abcd,"xyzasaa"
salt.strength,abcdef,"easy to find"
salt.color,abcdeffa,"not sweet"
salt.quality,abcd,"how much is needed"

如果任何一个description是多行的,上面将把它连接到一行。


推荐阅读