Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the yaml file to a csv format?

Tags:

shell

yaml

awk

jq

yq

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.🙂

like image 586
Clair Avatar asked Jan 24 '26 04:01

Clair


1 Answers

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
like image 147
pmf Avatar answered Jan 26 '26 00:01

pmf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!