Search code examples
shellawkyamljqyq

How to format the yaml file to a csv format?


I have used the yq command to format a yaml file:

cat includelist.yml |yq -r '.ProductLine.ADO_FeedsList'

output:

---
ProductLine:
  ProductLineName: AAAAA
  ADO_FeedsList:
  - ProjectName: IT
    FeedsName:
    - test
    - test2
  - ProjectName: organization
    FeedsName:
    - hello
    - world
---
ProductLine:
  ProductLineName: BBBBB
  ADO_FeedsList:
  - ProjectName: Fin
    FeedsName:
    - good
    - aaaa
  - ProjectName: organization
    FeedsName:
    - bbb
    - ccc

Could somebody give me some suggestion,I don't known how to convert to csv format.

My question: how to convert the content to the following format:

IT,test

IT,test2

orginazation,hello

orginazation,world

Fin,good

Fin,aaaa

organization,bbb

organization,ccc


Thanks a ton.🙂


Solution

  • If you are using mikefarah/yq anyway, you could directly output CSV using -o csv (instead of using -o json and calling jq):

    yq -o csv '.ProductLine.ADO_FeedsList[] | [.ProjectName] + (.FeedsName[] | [.])' includelist.yml
    
    IT,test
    IT,test2
    organization,hello
    organization,world
    Fin,good
    Fin,aaaa
    organization,bbb
    organization,ccc