【问题标题】:Convert YAML data to CSV using shell script [closed]使用 shell 脚本将 YAML 数据转换为 CSV [关闭]
【发布时间】: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 库的语言。

标签: bash shell csv yaml


【解决方案1】:

如果你跑了

<input grep -E '(name:|value:)' | \
sed -r 's/^ +//g;s/name: ip-address/~\nname: ip-address/g;s/(name: |value: )//g' | \
perl -pe 's/\n/,/g;s/~,/\n/g;' | \
perl -pe 's/,$//g' | \
grep -vE '^$' | \
mlr --ocsv -N altkv >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 (https://github.com/johnkerl/miller)

【讨论】:

  • 非常感谢 aborruso.. 在使用“mlr”之前产生线条的输出帮助了我,这正是我正在寻找的......使用 mlr 将线条形成结构化方式是一种增强和对我来说很好的学习。
  • 感谢您的时间。再次感谢。
  • @SSK 欢迎您,但请注意,您的输入不是有效的 YAML 文件 :)
  • 是的..我明白了...谢谢我会努力的..以除上面显示的 YAML 之外的任何其他最佳格式获取服务器输出。
猜你喜欢
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 2015-01-09
  • 1970-01-01
相关资源
最近更新 更多