Awk convert csv to xml optionnal tag and rename tag -



Awk convert csv to xml optionnal tag and rename tag -

name;num_tel;num_fixe;id_client;num_comd;email;city;date_liv gwenael;0998452223;1038431234;50c;12345;gwa@yahoo.fr;london;08/07/2015 judith;0954674487;1045227937;23d;78965;judith@yahoo.fr;toulouse;11/05/2015

from csv generate in output xml using awk command. need help 2 problems. tag name (name;num_tel;num_fixe;id_client;num_comd;email;) default value of csv , city;date_liv had been added user before generate csv , can add together more tag after default tag. objective output optionals tags in xml , 1 time had been input in csv seek utilize loop have many mistakes need help it. started @ position 7 because optional tag start @ place until end.

nf==1 (i=7;i<=nf;i++) { printf " <option>%s</option>\n" printf " <val>%s</val>\n" }

secondly how can rename tag exemple num_tel cell phone in awk script ?

<rows> <c> <client> <identity> <name> gwenael </name> <m> <num_fixe> 0998452223 </num_fixe> <contact> <cell phone>1038431234 </cell phone> <!--num_tel--!> </contact> <option>city</option> <value>london </value> <option>date_liv</option> <value>08/07/2015</value> <num_comd> 12345</num_comd> </m> </identity> <date_liv>08/07/2015</date_liv> <locomotion>car</locomotion> </client> </c> <d> <pro> <identity> <name> judith </name> <id_client>23d</id_client> <option>city</option> <value>paris </value> <option>date_liv</option> <value>08/05/2015</value> <num_fixe> 0998452223 </num_fixe> <company> <num_fixe>1045227937</num_fixe> </company> <num_comd> 12345</num_comd> </identity> <date_liv>08/07/2015</date_liv> <locomotion>car</locomotion> </pro> </d>

focusing on problem of adding custom fields output, utilize this. need insert these parts script in appropriate places:

# set field separator begin { fs=";" } # first line nr == 1 { (i=7; i<=nf; ++i) a[i] = $i # store field headers in array next # skip next line } # other lines { # print values along matching name array (i=7; i<=nf; ++i) printf "<option>%s</option><value>%s</value>", a[i], $i }

your desired output format isn't clear didn't utilize formatting characters in code add together newline after each <option>-<value> pair in loop using built-in ors (output record separator) variable:

printf "<option>%s</option><value>%s</value>%s", a[i], $i, ors

the advantage of using ors rather \n is platform-independent, meaning code should work on files utilize dos line endings in windows or unix line endings on other systems.

xml csv awk

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -