Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

awk convert csv to xml and wellform

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

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

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...