- 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?
推薦閱讀
- Mastering OpenLayers 3
- Qt 5 and OpenCV 4 Computer Vision Projects
- Windows系統(tǒng)管理與服務(wù)配置
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊)
- 營銷數(shù)據(jù)科學(xué):用R和Python進(jìn)行預(yù)測分析的建模技術(shù)
- C#程序設(shè)計(jì)教程
- MATLAB定量決策五大類問題
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實(shí)戰(zhàn)全流程詳解(高級進(jìn)階篇)
- RISC-V體系結(jié)構(gòu)編程與實(shí)踐(第2版)
- Instant Ext.NET Application Development
- Mastering Backbone.js
- Access 2010中文版項(xiàng)目教程
- Raspberry Pi Robotic Blueprints
- Java Web開發(fā)實(shí)例大全(基礎(chǔ)卷) (軟件工程師開發(fā)大系)
- HTML5游戲開發(fā)實(shí)戰(zhàn)