- Bash Cookbook
- Ron Brash Ganesh Naik
- 197字
- 2021-07-23 19:17:40
How to do it...
Let's begin our activity as follows:
- 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}
- 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.
- 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}
- Review the output and see how it easy it is! Are there areas you could improve on in both of the scripts?
推薦閱讀
- 零基礎(chǔ)搭建量化投資系統(tǒng):以Python為工具
- Python量化投資指南:基礎(chǔ)、數(shù)據(jù)與實戰(zhàn)
- CockroachDB權(quán)威指南
- Python數(shù)據(jù)分析入門與實戰(zhàn)
- Learning Bayesian Models with R
- Hadoop+Spark大數(shù)據(jù)分析實戰(zhàn)
- Learning Laravel 4 Application Development
- Python機器學(xué)習(xí)編程與實戰(zhàn)
- Vue.js 2 Web Development Projects
- Visual Basic程序設(shè)計習(xí)題與上機實踐
- Getting Started with Python
- 計算機應(yīng)用基礎(chǔ)(第二版)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Python Machine Learning Cookbook
- INSTANT Apache Hive Essentials How-to