AWK CSV转换XML和wellform [英] awk convert csv to xml and wellform

查看:147
本文介绍了AWK CSV转换XML和wellform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有一个问题,我需要改变循环

 名称; num_tel; num_fixe; id_client; num_comd;电子邮件;城市; date_liv
gwenael; 0998452223; 1038431234; 50; 12345; gwa@yahoo.fr;伦敦; 2015年8月7日
烫发; 0966442312; 1038453211; 31C; 654321; marcel@yahoo.fr;对; 2015年8月6日
朱迪思; 0954674487; 1045227937; 23D; 78965; judith@yahoo.fr;图卢兹; 2015年11月5日
保罗; 0998452223; 1038431234; 35X; 19945; paul@yahoo.fr;波尔多; 2015年1月4日
TOTO; 0966442312; 1038453211; 31Z; 994991; toto@yahoo.frNice; 2015年2月12日
玛丽; 0954674487; 1045227937; 23; 78944; marie@yahoo.fr;里尔; 2015年4月8日
雅克; 0998452223; 1038431234; 77℃; 18845; jacque@yahoo.fr;布鲁日; 2015年9月5日
trucmuche; 0966442312; 1038453211; 31Z; 666321; trucmuche@yahoo.fr;柏林,2015年10月4日
塔塔; 0954674487; 1045227937; 23D; 77965; tata@yahoo.fr;新纽约; 2015年8月7日

我有使用循环modifiy实际上awk脚本,不permish做
我需要的。
1)我需要改变环路到,如果条件,因为在动作我将不得不
在脚本,以从CSV帧标记名称添加标签名。

例如在剧本我都会有这是不
在CSV帧是
 在CSV到XML输出​​。

2)产生之前,用户可以使用默认的标签名称的CSV添加标签。
对于为例,让我们考虑标签城市和date_liv已被用户添加
就拿CSV(列7,8)的默认标签后的位置。
那么怎么可能给他们一个循环在7列添加到开始结束到XML?

3)是否有可能重命名标签?对于由为例命令num_comd。

 <行和GT;
 < C = id_client>
    <客户端>
                <同一性GT;
                            <名称>                                < M>
                                        < NUM> < / NUM>
                                        < num_tel> < / num_tel>
                                        < num_comd> < / num_comd>
                                < / M>
                < /身份>            <&运动GT;汽车< /运动>
< /客户>
< / C>
< /行>


解决方案

让我们通过编写一个脚本,做什么,我想我明白你想开始,然后你可以告诉我们什么它需要做不同的/额外的:

  $猫tst.awk
BEGIN {FS =;;打印<行和GT; \\ N}NR == 1 {为(i = 1; I< = NF;我++)F [$ i] =我;下一个 }{标签= $(F [id_client]); GSUB(/ [0-9] /,,标签)}标签==C的{type =客户; FLDS =num_tel num_comd}
标签==D的{type =亲; FLDS =id_client num_fixe}{
    拆分(名FLDS,N / /)
    printf的<%S:%S> \\ N,标签,$(F [id_client])
    printf的<%S> \\ N型
    打印<同一性GT;
    对于(i = 1; i的N;我++)
        printf的<%S>%S< /%S> \\ N,N [I],$(F [N [I]),N [I]
    打印< /身份>中
    printf的<&运动GT;%S< /运动> \\ N,运动
    printf的< /%S> \\ N型
    printf的< /%S> \\ n \\ n,标签
}END {打印< /行>中}

  $ AWK -v运动='车'-f tst.awk文件
<行和GT;c为C:50℃>
    <客户端>
        <同一性GT;
            <名称>&gwenael LT; /名称>
            < num_tel> 0998452223< / num_tel>
            < num_comd> 12345< / num_comd>
        < /身份>
        <&运动GT;汽车< /运动>
    < /客户>
< / C>< C:31C>
    <客户端>
        <同一性GT;
            <名称>&马塞尔LT; /名称>
            < num_tel> 0966442312< / num_tel>
            < num_comd> 654321< / num_comd>
        < /身份>
        <&运动GT;汽车< /运动>
    < /客户>
< / C>< D​​:23D>
    <正面和GT;
        <同一性GT;
            <名称>&朱迪思LT; /名称>
            < id_client> 23D< / id_client>
            < num_fixe> 1045227937< / num_fixe>
        < /身份>
        <&运动GT;汽车< /运动>
    < / PRO>
< / D>< /行>

现在 - 还有什么?它需要做的。

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>

解决方案

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>\n" }

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>\n", tag, $(f["id_client"])
    printf     "    <%s>\n", type
    print      "        <identity>"
    for (i=1;i in n;i++)
        printf "            <%s>%s</%s>\n", n[i], $(f[n[i]]), n[i]
    print      "        </identity>"
    printf     "        <locomotion>%s</locomotion>\n", locomotion
    printf     "    </%s>\n", type
    printf     "</%s>\n\n", 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?

这篇关于AWK CSV转换XML和wellform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆