I need to write bash script which takes yaml file:
first:John
last:Smith
first:David
last:Bennetand convert it to csv file:
John,Smith
David,BennetAny ideas? Thanks!
14 Answers
You will find a complete library for it and you can easily use it, YAML2CSV... see github , here is the link provided.
2Here is an awk command:
awk -v RS="first:|last:" '{gsub("first:|last:","",$0);print $1","$2}' RS='' infile.txtOutput 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.csvyou will have
John,Smith
David,BennetPlease 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.csvExplanation :
<file.yamlis the input file (here, your YAML file to convert)sed 's/first://g'would remove eachfirst:string list charactersed ':a;N;$!ba;s/\nlast:/,/g'do almost same thing as previous command, but all options are used to detect newlines character which are previouslast: string character lists (and replace that with a comma,`); see this StackOverflow answer more further detailssed -r '/^\s*$/d'would remove each empty lines left, see this StackOverflow answer for more details> file.csvwould redirect result into a new.csvfile (if this file didn't exist, it would be created automatically), as conversion is finished
Result :
John,Smith
David,BennetCreate 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"
fiand to execute it (don't forget to make the script executable with the command sudo chmod +x YAMLtoCSV.sh) :
./YAMLtoCSV.sh /home/<user>/file.yamlYou 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)