How to do it...

Let's begin our activity as follows:

  1. Open a terminal and create the data-csv-to-xml.sh script with the following contents. Then, execute the script after saving it using $ bash data-csv-to-xml.sh:
#!/bin/bash# Import template variablessource xml-parent.tplsource word.tplOUTPUT_FILE="words.xml"INPUT_FILE="words.csv"DELIMITER=','# Setup headerecho ${XML_HDR} > ${OUTPUT_FILE}echo ${SRT_CONTR} >> ${OUTPUT_FILE}# Enter contentecho ${ELM} | \sed '{:q;N;s/\n/\\n/g;t q}'| \awk \'{ print "awk \x27 BEGIN{FS=\"'${DELIMITER}'\"}{print "$0"}\x27 '${INPUT_FILE}'"}' | \ sh >> ${OUTPUT_FILE}# Append trailerecho ${END_CONTR} >> ${OUTPUT_FILE}cat ${OUTPUT_FILE}
  1. Examine the output, but be aware that "pretty" XML isn't necessary and in fact, ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.