Ubuntu – How to convert yaml to csv file format

bashcsvtext processing

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!

Best Answer

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