bash - 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
解决方案
在任何 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
是多行的,上面将把它连接到一行。
推荐阅读
- ruby-on-rails - 生成 rails 控制器之后启动服务器
- ios - 在不同的 ViewController 之间传递数据
- r - 如何比较 r 中两个数据框的值并在 R 中创建一个新的数据框作为结果
- javascript - 前端防病毒扫描文件上传
- keras - 增强合成数据与不增强合成数据但在批次上增强的训练?
- python - python rest保存解析的xml文档-错误编码
- java - FTPClient listFiles 方法正在返回目录。我该如何过滤?
- php - 渲染模板期间抛出异常(“警告:字符串转换时数据丢失”)
- android - recyclerview onswipe 项目的运动布局(alpha4)问题
- markdown - 将 DOCX 转换为 Markdown 无法放置图像