Bash 使用shell脚本将YAML数据转换为CSV
我有一个文本文件,其中包含一个YAML对象,其中包含通过访问远程服务器生成的重复密钥Bash 使用shell脚本将YAML数据转换为CSV,bash,shell,csv,yaml,Bash,Shell,Csv,Yaml,我有一个文本文件,其中包含一个YAML对象,其中包含通过访问远程服务器生成的重复密钥 --- response: data: configElement: elementType: steering-pool attribute: name: ip-address value: 10.10.10.1 attribute: name: start-port value: 100
---
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的数据
我已经尝试过Solution proposed on,但它不适合我的文本数据中使用的格式
预期输出如下所示,将configElement中的所有值放在一起:
请帮忙
提前感谢。如果您跑步
output.csv
你会有
+------------+------------+----------+----------+-------------------+-------------------+--------------------+
| ip-address | start-port | end-port | realm-id | network-interface | last-modified-by | last-modified-date |
+------------+------------+----------+----------+-------------------+-------------------+--------------------+
| 10.10.10.1 | 100 | 9000 | TSTore | M10:150 | admin@10.10.10.10 | 2018-01-23 10:11 |
| 10.10.10.2 | 1000 | 6535 | TSTLAN | M10:278 | admin@10.10.10.10 | 2010-01-07 11:28 |
| 10.10.10.3 | 8000 | 9535 | TestLab2 | M10:332 | admin@10.10.10.10 | 2009-02-10 12:14 |
| 10.10.10.5 | 100 | 435 | NEWLA15 | M10:253 | admin@10.10.10.10 | 2016-02-22 17:32 |
| 10.10.10.4 | 100 | 655 | LAN325 | M10:2153 | admin@10.10.10.10 | 2019-04-20 13:12 |
+------------+------------+----------+----------+-------------------+-------------------+--------------------+
最后一个实用程序-mlr-是Miller如果它不是真正的YAML,就不要称它为YAML。我想说你有一个结构化的文本文件要转换成CSV格式更合适。如果你是通过访问远程服务器生成的,为什么不使用预期的格式生成它呢?我正在努力放置数据…,是的,请更新你的Q以显示你的努力。我们很高兴帮助您提高对脚本的理解,但我们不想为您这样做。如果您可以减少示例输入的大小,那么如果它是10个属性还是只有2个属性,这到底有什么关系?如果你能尽最大努力解决你的问题,你会得到很多帮助。Good luck.YAML对象与JSON对象一样,可以有重复的键;通常(但不一定)将对象解码为需要唯一密钥的数据结构。我对问题进行了编辑以反映这一点。不过,Shell语言不是解析YAML数据的正确语言。使用提供YAML库的语言。非常感谢Aboruso。。在使用“mlr”之前产生行的输出帮助了我,这正是我所寻找的…使用mlr将行形成结构化的方式对我来说是一种增强和良好的学习。感谢您的时间。再次感谢。@SSK不客气,但请注意,您的输入不是有效的YAML文件:是。。我得到了它。。。谢谢,我会努力的。。以除上述YAML以外的任何其他最佳格式获取服务器输出。
+------------+------------+----------+----------+-------------------+-------------------+--------------------+
| ip-address | start-port | end-port | realm-id | network-interface | last-modified-by | last-modified-date |
+------------+------------+----------+----------+-------------------+-------------------+--------------------+
| 10.10.10.1 | 100 | 9000 | TSTore | M10:150 | admin@10.10.10.10 | 2018-01-23 10:11 |
| 10.10.10.2 | 1000 | 6535 | TSTLAN | M10:278 | admin@10.10.10.10 | 2010-01-07 11:28 |
| 10.10.10.3 | 8000 | 9535 | TestLab2 | M10:332 | admin@10.10.10.10 | 2009-02-10 12:14 |
| 10.10.10.5 | 100 | 435 | NEWLA15 | M10:253 | admin@10.10.10.10 | 2016-02-22 17:32 |
| 10.10.10.4 | 100 | 655 | LAN325 | M10:2153 | admin@10.10.10.10 | 2019-04-20 13:12 |
+------------+------------+----------+----------+-------------------+-------------------+--------------------+