【发布时间】:2020-06-13 22:58:00
【问题描述】:
我有一个文本文件,其中包含一个 YAML 对象,其中包含我通过访问远程服务器生成的重复键。
---
response:
data:
configElement:
elementType: steering-pool
attribute:
name: ip-address
value: 10.10.10.1
attribute:
name: start-port
value: 100
attribute:
name: end-port
value: 9000
attribute:
name: realm-id
value: TSTore
attribute:
name: network-interface
value: M10:150
attribute:
name: last-modified-by
value: admin@10.10.10.10
attribute:
name: last-modified-date
value: 2018-01-23 10:11
configElement:
elementType: steering-pool
attribute:
name: ip-address
value: 10.10.10.2
attribute:
name: start-port
value: 1000
attribute:
name: end-port
value: 6535
attribute:
name: realm-id
value: TSTLAN
attribute:
name: network-interface
value: M10:278
attribute:
name: last-modified-by
value: admin@10.10.10.10
attribute:
name: last-modified-date
value: 2010-01-07 11:28
configElement:
elementType: steering-pool
attribute:
name: ip-address
value: 10.10.10.3
attribute:
name: start-port
value: 8000
attribute:
name: end-port
value: 9535
attribute:
name: realm-id
value: TestLab2
attribute:
name: network-interface
value: M10:332
attribute:
name: last-modified-by
value: admin@10.10.10.10
attribute:
name: last-modified-date
value: 2009-02-10 12:14
configElement:
elementType: steering-pool
attribute:
name: ip-address
value: 10.10.10.5
attribute:
name: start-port
value: 100
attribute:
name: end-port
value: 435
attribute:
name: realm-id
value: NEWLA15
attribute:
name: network-interface
value: M10:253
attribute:
name: last-modified-by
value: admin@10.10.10.10
attribute:
name: last-modified-date
value: 2016-02-22 17:32
configElement:
elementType: steering-pool
attribute:
name: ip-address
value: 10.10.10.4
attribute:
name: start-port
value: 100
attribute:
name: end-port
value: 655
attribute:
name: realm-id
value: LAN325
attribute:
name: network-interface
value: M10:2153
attribute:
name: last-modified-by
value: admin@10.10.10.10
attribute:
name: last-modified-date
value: 2019-04-20 13:12
messages:
links:
我正在努力根据其结构将数据放入行中。它在标签configElement下有多个元素。
使用shell脚本,我想将数据写入行,每行包含configElement的数据。
我已经尝试过https://stackoverflow.com/a/21189044/8763301 提出的解决方案,但它不适合我的文本数据中使用的格式。
预期的输出如下,将configElement中的所有值放在一起:
attribute: 10.10.10.1,100,9000,TSTore,M10:150,admin@10.10.10.10,"2018-01-23 10:11"
.
.
.
attribute: 10.10.10.2,1000,6535,TSTLAN,M10:278,admin@10.10.10.10,"2010-01-07 11:28"
请帮忙。
提前致谢。
【问题讨论】:
-
如果它不是真正的
YAML,就不要叫它YAML。我想说你有一个结构化的文本文件要转换为CSV格式会更合适。 -
如果你“通过访问远程服务器生成”为什么不使用预期的格式生成它呢?
-
“我正在努力放置数据...”,是的,请更新你的 Q 来展示你的挣扎。我们很高兴帮助您提高对脚本的理解,但我们不想为您做这件事。如果您可以减少样本输入的大小(如果它是 10 个属性还是只有 2 个,这真的很重要?)并展示您解决问题的最佳尝试,您将获得大量帮助。祝你好运。
-
YAML 对象,和 JSON 对象一样,可以有重复的键;通常,但不一定,对象被解码为需要唯一键的数据结构。我已编辑问题以反映这一点。
-
但是,Shell 语言不是用于解析 YAML 数据的正确语言。使用提供 YAML 库的语言。