How to convert yaml to csv file format?

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!

1

4 Answers

You will find a complete library for it and you can easily use it, YAML2CSV... see github , here is the link provided.

2

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

Using sed and 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.

You can use sed command to do this conversion :

 <file.yaml sed 's/first://g' | sed ':a;N;$!ba;s/\nlast:/,/g' | sed -r '/^\s*$/d' > file.csv

Explanation :

  • <file.yaml is the input file (here, your YAML file to convert)
  • sed 's/first://g' would remove each first: string list character
  • sed ':a;N;$!ba;s/\nlast:/,/g' do almost same thing as previous command, but all options are used to detect newlines character which are previous last: string character lists (and replace that with a comma,`); see this StackOverflow answer more further details
  • sed -r '/^\s*$/d' would remove each empty lines left, see this StackOverflow answer for more details
  • > file.csv would redirect result into a new .csv file (if this file didn't exist, it would be created automatically), as conversion is finished

Result :

John,Smith
David,Bennet

Create your own script which will able to convert any .yaml file to .csv file. In my example, my script is named YAMLtoCSV.sh :

#! /bin/bash
if [ ${1#*.} = "yaml" ]
then <$1 sed 's/first://g' | sed ':a;N;$!ba;s/\nlast:/,/g' | sed -r '/^\s*$/d' > ${1%%.*}.csv echo "Conversion from" $1 "to" ${1%%.*}.csv "done"
else echo "This file didn't have YAML extension"
fi

and to execute it (don't forget to make the script executable with the command sudo chmod +x YAMLtoCSV.sh) :

./YAMLtoCSV.sh /home/<user>/file.yaml

You would get a file named file.csv next to your .yaml file, so his path is /home/<user>/file.csv

Note that I added an if statement in order to prevent file conversion error (which won't destroy any data, but would create some useless other data)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like