csv到xml命令行和条件 [英] csv to xml command line and condition

查看:290
本文介绍了csv到xml命令行和条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用awk将csv转换为xml文件。为了更好地理解我给出我的csv文件的例子:

I am trying to convert a csv to a xml file using awk. To better understand i give a exemple of my 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

我的目标是拿第三个字母id_client(C,D,X,Z),如果例如:
字母是C在xml中,我将只有标签名称,num_comd和tel_fixe。
如果字母是D,我将有标签名称,id_client,num_fixe

My objective is to take the third letter of id_client (C,D,X,Z), and if for example: the letter is C in the xml i will have the tag name, num_comd and tel_fixe only . If the letter is D i will have the tag name, id_client, num_fixe

我成功使用awk脚本管道与命令剪切,并把它在变量
现在i'am在这里:

I succeed in taking the letter with awk script pipe with command cut and put it in a variable now i'am here :

 if(var==C) 
  {}
Else if (var==D)
{}

我请解决有关如何输入正确的xml标签的问题?
我使用命令行很新。

Can you help me please to resolve problem about how input the right xml tag? I am pretty new with command line.

抱歉的写作错误。我是法语。

Sorry for the writing mistakes. I am french.

推荐答案

这里是一个可执行的awk脚本示例,其中我创建了几个帮助函数来重用代码: p>

Here's an executable awk script example, where I created several helper functions to 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 ) )
}

在文件( script.awk )中,使其可执行,然后像

You could put this in a file (script.awk), make it executable and then run it like

./script.awk data > data.xml

其中 data 我给你的数据文件, data.xml 是数据将结束的地方。

where data is the name I gave your data file, and data.xml is where the data will end up.

<?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>

我相信我没有对XML文件的各个部分使用正确的术语,但此输出可由浏览器作为有效(可分析)XML加载。

I'm sure I'm not using the correct terminology for the various parts of the XML file, but this output can be loaded by a browser as "valid"(parseable) XML. You'd probably want to look into a DTD if this file should be give to anyone else.

以下是一般分类:


  • BEGIN 块中,将字段分隔符设置为; 为每个对象和 documentEnclosingTag 初始化变量,例如所需的输出字段,然后打印一个通用的XML头以及 documentEnclosingTag

  • FNR == 1 块中,将数据的第一行作为标题,并将其放入 cols 数组,因此flds可以通过它们的头名称来引用。使用 gsub 调用清除数据以修剪列名称

  • 对于每个 $(cols [id_client ])块,通过指定enclosingTag值并匹配所需的flds数组,打印一个适当的Object。

  • END ,打印结束 documentEnclosingTag

  • In the BEGIN block, set the field separator to ;, initialize variables like the desired output field for each "Object" and documentEnclosingTag, then print a generic XML header along with the documentEnclosingTag
  • In the FNR==1 block, take the first row of the data as the headers and put them into the cols array so flds can be referenced by their header names later on. Clean the data with a gsub call to trim the column names
  • For each $(cols["id_client"]) block, print an appropriate "Object", by specifying an enclosingTag value and the matching desired flds array.
  • In the END, print the closing documentEnclosingTag

对于函数:


  • wrapData 创建一个包含数据周围标签的字符串。

  • createObject 使用 wrapData 创建元素和对象本身,在那里我添加了一点输出格式化使用awk的标准字符串连接。 flds 数组表示所需的输出字段。 s 是函数的局部变量。每个 fld ,数据和标签连接到 s ,然后最终 s enclosingTag 包装并作为字符串返回。

  • wrapData creates a string with enclosing tags around the data.
  • createObject uses wrapData to create the elements and the enclosing tags for the "Object" itself, where I added a little output formatting using awk's standard string concatenation. The flds array indicates the desired output fields. The key and s are local variables for the function. Each fld, data and tag are concatenated to s and then finally s is wrapped by the enclosingTag and returned as a string.

这篇关于csv到xml命令行和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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