csv to xml command line and condition -
i trying convert csv xml file using awk. better understand give exemple of csv file:
name;num_tel;num_fixe;id_client;num_comd gwenael;0998452223;1038431234;50c;12345 marcel;0966442312;1038453211;31c;654321 judith;0954674487;1045227937;23d;78965 paul;0998452223;1038431234;35x;19945 toto;0966442312;1038453211;31z;994991 marie;0954674487;1045227937;23c;78944 jacque;0998452223;1038431234;77c;18845 trucmuche;0966442312;1038453211;31z;666321 tata;0954674487;1045227937;23d;77965 my objective take third letter of id_client (c,d,x,z), , if example: letter c in xml have tag name, num_comd , tel_fixe . if letter d have tag name, id_client, num_fixe
i succeed in taking letter awk script pipe command cut , put in variable i'am here :
if(var==c) {} else if (var==d) {} can me please resolve problem how input right xml tag? pretty new command line.
sorry writing mistakes. french.
here's executable awk script example, created several helper functions re-use code:
#!/usr/bin/awk -f begin { fs=";" documentenclosingtag = "rows" c_flds["name"]; c_flds["num_comd"]; c_flds["num_tel"] d_flds["name"]; d_flds["id_client"]; d_flds["num_fixe"] x_flds["name"] z_flds["name"] print "<?xml version=\"1.0\" encoding=\"utf-8\"?>" printf "<%s>\n", documentenclosingtag } fnr==1 { gsub(" ", ""); for(i=1; i<=nf; i++) cols[$i]=i; next } $(cols["id_client"]) ~ /c$/ { print createobject( "c", c_flds ) } $(cols["id_client"]) ~ /d$/ { print createobject( "d", d_flds ) } $(cols["id_client"]) ~ /x$/ { print createobject( "x", x_flds ) } $(cols["id_client"]) ~ /z$/ { print createobject( "z", z_flds ) } end { printf "</%s>\n", documentenclosingtag } #----------- functions ----------- function createobject( enclosingtag, flds, key, s) { for(key in flds) { s = s "\t" wrapdata( key, $(cols[key]) ) "\n" } return( wrapdata( enclosingtag, "\n" s ) ) } function wrapdata( enclosingtag, data ) { return( sprintf( "<%s>%s</%s>", enclosingtag, data, enclosingtag ) ) } you put in file (script.awk), make executable , run like
./script.awk data > data.xml where data name gave data file, , data.xml data end up.
the output like:
<?xml version="1.0" encoding="utf-8"?> <rows> <c> <num_tel>0998452223</num_tel> <name>gwenael</name> <num_comd>12345 </num_comd> </c> <c> <num_tel>0966442312</num_tel> <name>marcel</name> <num_comd>654321 </num_comd> </c> <d> <id_client>23d</id_client> <num_fixe>1045227937</num_fixe> <name>judith</name> </d> <x> <name>paul</name> </x> <z> <name>toto</name> </z> <c> <num_tel>0954674487</num_tel> <name>marie</name> <num_comd>78944</num_comd> </c> <c> <num_tel>0998452223</num_tel> <name>jacque</name> <num_comd>18845</num_comd> </c> <z> <name>trucmuche</name> </z> <d> <id_client>23d</id_client> <num_fixe>1045227937</num_fixe> <name>tata</name> </d> </rows> i'm sure i'm not using correct terminology various parts of xml file, output can loaded browser "valid"(parseable) xml. you'd want dtd if file should give else.
here's general breakdown:
- in
beginblock, set field separator;, initialize variables desired output field each "object" ,documentenclosingtag, print generic xml header alongdocumentenclosingtag - in
fnr==1block, take first row of data headers , put themcolsarray flds can referenced header names later on. clean datagsubcall trim column names - for each
$(cols["id_client"])block, print appropriate "object", specifying enclosingtag value , matching desired flds array. - in
end, print closingdocumentenclosingtag
as functions:
wrapdatacreates string enclosing tags around data.createobjectuseswrapdatacreate elements , enclosing tags "object" itself, added little output formatting using awk's standard string concatenation.fldsarray indicates desired output fields.key,slocal variables function. eachfld, data , tag concatenateds,swrappedenclosingtag, returned string.
Comments
Post a Comment