Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

One more question, i need to change the loop

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
marcel;0966442312;1038453211;31C;654321;marcel@yahoo.fr;Pairs;08/06/2015
judith;0954674487;1045227937;23D;78965;judith@yahoo.fr;Toulouse;11/05/2015
paul;0998452223;1038431234;35X;19945;paul@yahoo.fr;Bordeaux;01/04/2015
toto;0966442312;1038453211;31Z;994991;toto@yahoo.frNice;02/12/2015 
marie;0954674487;1045227937;23C;78944;marie@yahoo.fr;Lille;04/08/2015
jacque;0998452223;1038431234;77C;18845;jacque@yahoo.fr;Bruges;09/05/2015
trucmuche;0966442312;1038453211;31Z;666321;trucmuche@yahoo.fr;Berlin;10/04/2015 
tata;0954674487;1045227937;23D;77965;tata@yahoo.fr;New-york;08/07/2015

i have to modifiy the awk script in fact using a loop for does not permish to do what i need. 1) I need to change the loop for to a if condition because in the action i will have to add tag name in the script in order to frame tag name from the csv.

for example in the script i will have which is not in the csv to frame which is in the csv to output in xml.

2) User can add tags in the csv with the default tags name before it is generated. for exemple let's consider that tags city and date_liv had been added by user so they took position after the default tags of the csv (column 7 and 8). So how is it possible to add them with a loop starting at column 7 to the end to the xml ?

3) is it possible to rename a tag ? for exemple num_comd by command.

 <rows>
 <C = id_client>
    <client> 
                <identity>              
                            <name>  

                                <M>
                                        <num> </num>
                                        <num_tel> </num_tel>
                                        <num_comd> </num_comd>
                                </M>                                    
                </identity>

            <locomotion>car</locomotion>
</client>
</C>
</rows>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
367 views
Welcome To Ask or Share your Answers For Others

1 Answer

Let's start by writing a script that does what I think I understand you want and then you can tell us what it needs to do differently/additionally:

$ cat tst.awk                             
BEGIN { FS=";"; print "<rows>
" }

NR==1 { for (i=1; i<=NF; i++) f[$i]=i; next }

{ tag = $(f["id_client"]); gsub(/[0-9]/,"",tag) }

tag == "C" { type="client"; flds="num_tel num_comd" }
tag == "D" { type="pro"; flds="id_client num_fixe" }

{
    split("name "flds,n,/ /)
    printf     "<%s: %s>
", tag, $(f["id_client"])
    printf     "    <%s>
", type
    print      "        <identity>"
    for (i=1;i in n;i++)
        printf "            <%s>%s</%s>
", n[i], $(f[n[i]]), n[i]
    print      "        </identity>"
    printf     "        <locomotion>%s</locomotion>
", locomotion
    printf     "    </%s>
", type
    printf     "</%s>

", tag
}

END { print "</rows>" }

.

$ awk -v locomotion='car' -f tst.awk file
<rows>

<C: 50C>
    <client>
        <identity>
            <name>gwenael</name>
            <num_tel>0998452223</num_tel>
            <num_comd>12345</num_comd>
        </identity>
        <locomotion>car</locomotion>
    </client>
</C>

<C: 31C>
    <client>
        <identity>
            <name>marcel</name>
            <num_tel>0966442312</num_tel>
            <num_comd>654321</num_comd>
        </identity>
        <locomotion>car</locomotion>
    </client>
</C>

<D: 23D>
    <pro>
        <identity>
            <name>judith</name>
            <id_client>23D</id_client>
            <num_fixe>1045227937</num_fixe>
        </identity>
        <locomotion>car</locomotion>
    </pro>
</D>

</rows>

Now - what else does it need to do?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...