How to convert yaml to csv file format?

6,192

Solution 1

You will find a complete library for it and you can easily use it, YAML2CSV... see github , here is the link provided. https://github.com/tokland/yaml2csv

Solution 2

Using sed and Miller (https://github.com/johnkerl/miller) and running:

<input.yaml sed -r 's/:/ /g' | mlr --x2c -N cat >output.csv

you will have

John,Smith
David,Bennet

Please note: your input file is not a valid yaml.

Solution 3

Here is an awk command:

awk -v RS="first:|last:" '{gsub("first:|last:","",$0);print $1","$2}' RS='' infile.txt

Output is as you are expecting:

John,Smith
David,Bennet
Share:
6,192
Tzvika Avni
Author by

Tzvika Avni

Updated on September 18, 2022

Comments

  • Tzvika Avni
    Tzvika Avni over 1 year

    I need to write bash script which takes yaml file:

    first:John
    last:Smith
    
    first:David
    last:Bennet
    

    and convert it to csv file:

    John,Smith
    David,Bennet
    

    Any ideas? Thanks!

    • aborruso
      aborruso over 4 years
      this is not a valid yaml content. You should have it in this way pastebin.com/KEffBae3
  • AlwaysTalkingAboutMyDog
    AlwaysTalkingAboutMyDog almost 6 years
    Can you provide an example of the library in addition to the link you provided?
  • George Udosen
    George Udosen over 4 years
    How do we install and use this from a terminal window?