Convert CSV to XML

Source: Internet
Author: User

1. CSV is a simple table file format defined, which is similar to excel but easier than it. Open the CSV file in a text editor and you can see
In fact, "," is basically used to split the data of each field, as follows:

A, 32, 0, 1, communication station, switch room on the eighth floor. East Gate magnetic
A, 32, 1, 1, communication station, switch room on the eighth floor. Ximen gate magnetic
A, 32, 2, 0, communication station, switch room on the eighth floor. Communication screen fault
A, 32, 3, 0, communication station, switch room on the eighth floor. ups running status
A, 32, 4, 0, communication station, switch room on the eighth floor. High Humidity alarm

 

2. If you do not need to consider complicated situations, you can modify the bash script as needed and convert the above CSV file (I changed it to a TXT file)
Convert the XML file (fep_yx.txt-> an $1/the first of the last field. The previous name/$2 file path $1 _ $ 2.xml ):
E.g. Convert the preceding section to switch room/a_32.xml on the/8 floor.

#! /Usr/bin/bash <br/> export lc_all = zh_cn.gbk <br/> bar = "Hangzhou" <br/> # obtain the non-repeated enumeration of the first column <br/> FePS = "$ (cat fep_yx.txt | awk 'in in {FS = ", "} {print $1} '| uniq | grep' ^ [A-Z] ')" <br/> echo "FePS supported des: "$ FePS <br/> echo $ bar <br/> # create a folder for each enumeration <br/> for FEP in $ FePS; DO <br/> echo $ bar <br/> echo "FEP: "$ FEP <br/> [-d $ FEP] | mkdir-p $ FEP </P> <p> touch $ fe P/FEP <br/> RM-RF. /$ FEP/* </P> <p> # filter out the first column of the total file and put it in the same folder. <br/> CAT fep_yx.txt | grep "^ $ FEP">. /$ FEP "_ Yx ". TXT <br/> CAT fep_yc.txt | grep "^ $ FEP">. /$ FEP "_ YC ". TXT </P> <p> # Find the first field. previous name <br/> Devs = $ (cat. /"$ FEP"/$ FEP "_ Yx ". TXT | awk 'in in {FS = "," }{ Print $6} '| awk' begin {FS = ". "} {print $1} '| uniq) <br/> for Dev in $ Devs; DO <br/> # echo $ Dev <br/> echo $ Dev> $ FEP/_dev.txt <br/> done <br /> Devs = $ (cat. /"$ FEP"/"$ FEP" _yc.txt | awk 'in in {FS = ","} {print $6} '| awk' begin {FS = ". "} {print $1} '| uniq) <br/> for Dev in $ Devs; DO <br/> echo $ Dev >$ FEP/_dev.txt <br/> done </P> <p> Devs =$ (cat $ FEP/_dev.txt | uniq) </P> <p> for Dev in $ Devs; DO </P> <p> [$ Dev = "0"] & break; </P> <p> echo "--------------- Dev: -"$ Dev </P> <p> [-d $ FEP/$ Dev] | mkdir-p $ FEP/$ Dev </P> <p> cat. /"$ FEP"/"$ FEP" _yx.txt | Grep "$ Dev" | sed "s/$ DEV /. // G ">. /$ FEP/$ DEV/"$ FEP" _yx.txt <br/> cat. /"$ FEP"/"$ FEP" _yc.txt | grep "$ Dev" | sed "s/$ DEV /. // G ">. /$ FEP/$ DEV/"$ FEP" _yc.txt </P> <p> # locate the different enumerations of $2 in the above layers <br/> Stas =$ (cat $ FEP /$ DEV/"$ FEP" _yx.txt | awk 'in in {FS = ", "} {print $2} '| uniq | grep' ^ [0-9] * ') # <$ (cat $ FEP/$ DEV/fep_yc.txt | awk 'in in {FS = ", "} {print $2} '| uniq | grep' ^ [0-9] * ') | uniq) <br/> echo" sta: -- "$ Stas <br/> For STA in $ Stas; DO <br/> echo "nod: ---" "$ FEP, $ Dev, $ sta "</P> <p> # Start to convert to XML <br/> echo" <? XML version =/"1.0/" encoding =/"gb2312/" standalone =/"No/"?> ">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> echo "<Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML </P> <p> CAT $ FEP/$ DEV/"$ FEP" _yx.txt | grep "$ sta">. /"$ FEP"/$ DEV/"$ FEP" _ "$ sta" _yx.txt <br/> echo "<Switch Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> cat. /"$ FEP"/$ DEV/"$ FEP" _ "$ sta" _yx.txt | awk 'in in {FS = ", "}{ print" <row> <Name> "$6" </Name> <alarm_value> "$4" </alarm_value> <alarm_degree> 162 </alarm_degree> <Node> "$3" </node> </row> "} '>. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> echo "</Switch Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML </P> <p> RM $ FEP/$ DEV/"$ FEP" _ "$ sta" _yx.txt </P> <p> CAT $ FEP/$ DEV /" $ FEP "_yc.txt | grep" $ sta ",>. /"$ FEP"/$ DEV/"$ FEP" _ "$ sta" _yc.txt <br/> echo "<analog Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> cat. /"$ FEP"/$ DEV/"$ FEP" _ "$ sta" _yc.txt | awk 'in in {FS = ", "}{ print" <row> <Name> "$16" </Name> <up_bound> "$7" </up_bound> <up_degree> "$9" </up_degree> <low_bound> "$8" </low_bound> <low_degree> "$10" </low_degree> <up_up_bound> "$11" </up_up_bound> <up_up_degree> "</up_up_degree> <low_low_bound>" $12 "</low_low_bound> <low_low_degree>" $14 "</low_degree> <node>" $3 "</node> <OFFSET> "$6" </Offset> <calc_method> "$4" </calc_method> <coeff> "$5" </coeff> </row> "} '>. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> echo "</analog Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML <br/> echo "</Point Information>">. /$ FEP/$ DEV/"$ FEP" _ "$ sta ". XML </P> <p> RM $ FEP/$ DEV/"$ FEP" _ "$ sta" _yc.txt <br/> # clear intermediate files <br/> RM-RF. /$ FEP/$ DEV/"$ FEP" _yx.txt. /$ FEP/$ DEV/"$ FEP" _yc.txt </P> <p> done </P> <p> RM-F $ FEP/_dev.txt $ FEP/"$ FEP "_yx.txt $ FEP/" $ FEP "_yc.txt </P> <p> done 

 

3. converted results

<? XML version = "1.0" encoding = "gb2312" standalone = "no"?> <Br/> <Point Information> <br/> <Switch Point Information> <br/> <row> <Name> dongmen field </Name> <alarm_value> 1 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 0 </node> </row> <br/> <row> <Name> Ximen door magnetism </Name> <alarm_value> 1 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 1 </node> </row> <br/> <row> <Name> communication screen fault </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 2 </node> </row> <br/> <row> <Name> UPS running status </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 3 </node> </row> <br/> <row> <Name> high humidity alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 4 </node> </row> <br/> <row> <Name> low humidity alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 5 </node> </row> <br/> <row> <Name> high temperature alert </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 6 </node> </row> <br/> <row> <Name> low temperature alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </ alarm_degree> <node> 7 </node> </row> <br/> <row> <Name> alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 16 </node> </row> <br/> <row> <Name> distribution frame A0 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 17 </node> </row> <br/> <row> <Name> distribution frame A1 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 18 </node> </row> <br/> <row> <name> distribution frame A2 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 19 </node> </row> <br /> <row> <Name> distribution frame A3 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 20 </node> </row> <br/> <row> <Name> distribution frame A4 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <Node> 21 </node> </row> <br/> <row> <Name> distribution frame A5 column alarm </Name> <alarm_value> 0 </alarm_value> <alarm_degree> 162 </alarm_degree> <node> 22 </node> </row> <br/> </Switch Point Information> <br/> <analog Point Information> <br/> <row> <Name> temperature </Name> <up_bound> 35.000 </up_bound> <up_degree> 163 </up_degree> <low_bound> 5.000 </low_bound> <low_degree> 163 </ low_degree> <up_up_bound> 40.000 </up_up_bound> <strong> 0 </strong> <low_low_bound>-2.000 </low_low_bound> <low_low_degree> 162 </strong> <node> 0 /node> <OFFSET> 0.000 </Offset> <calc_method> 0 </calc_method> <coeff>-1.2207e-002 </coeff> </row> <br/> <row> <Name> humidity </Name> <up_bound> 95.000 </up_bound> <up_degree> 164 </up_degree> <low_bound> 30.000 </low_bound> <low_degree> 164 </low_degree> <up_up_bound> 100.000 </up_up_bound> <strong> 1 </strong> <low_low_bound> 10.000 </low_low_bound> <low_low_degree> 162 </strong> <node> 1 </node> <OFFSET> 0.000 </Offset> <calc_method> 0 </calc_method> <coeff>-2.4414e-002 </coeff> </row> <br/> </analog point information> <br/> </Point Information> 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.