- 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?
推薦閱讀
- 精通JavaScript+jQuery:100%動態網頁設計密碼
- GraphQL學習指南
- Learning Apex Programming
- 深入淺出Java虛擬機:JVM原理與實戰
- Java 9 Programming Blueprints
- C語言實驗指導及習題解析
- Apex Design Patterns
- Expert Data Visualization
- 單片機C語言程序設計實訓100例
- 零基礎學Python編程(少兒趣味版)
- Mastering Docker
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- PostgreSQL 12 High Availability Cookbook
- Qt 5.12實戰
- Spring Boot 2+Thymeleaf企業應用實戰