官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 197字
  • 2021-07-23 19:17:40

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 variables
source xml-parent.tpl
source word.tpl

OUTPUT_FILE="words.xml"
INPUT_FILE="words.csv"
DELIMITER=','

# Setup header
echo ${XML_HDR} > ${OUTPUT_FILE}
echo ${SRT_CONTR} >> ${OUTPUT_FILE}

# Enter content
echo ${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 trailer
echo ${END_CONTR} >> ${OUTPUT_FILE}

cat ${OUTPUT_FILE}
  1. Examine the output, but be aware that "pretty" XML isn't necessary and in fact, we don't even need to have the XML on multiple lines. If pure data is required for a web application, the extra new lines and tabs are unnecessary data to be transmitted.
  2. Create another script named data-xml-to-json.sh with the following contents. Then, execute the script after saving it using  data-xml-to-json.sh:
!#/bin/bash
INPUT_FILE"words.xml"
OUTPUT_FILE="words.json"

# Easy one line!
xml2json < ${INPUT_FILE} ${OUTPUT_FILE}
  1. Review the output and see how it easy it is! Are there areas you could improve on in both of the scripts?
主站蜘蛛池模板: 长岭县| 湘西| 灵川县| 凤翔县| 芦山县| 禹城市| 嘉祥县| 勐海县| 云浮市| 普格县| 蒙阴县| 东乌珠穆沁旗| 荥阳市| 兴安盟| 陆河县| 高尔夫| 岑溪市| 翼城县| 南城县| 和政县| 河南省| 平远县| 扶风县| 固阳县| 乐东| 突泉县| 宁津县| 唐河县| 云南省| 丰镇市| 元江| 舟曲县| 平武县| 东乡族自治县| 太仓市| 肇东市| 金寨县| 慈利县| 项城市| 西充县| 哈巴河县|