我有一个巨大的XML文件,我想分裂的基础上,最少的记录,即元素。每个拆分文件100。
在这个例子中,xml只有两条记录,所以awk应该为每个stuRec创建一个xml文件,因为我把最小文件数硬编码为1。
但我得到了错误:“awk:illegal statement at source line 1”我不知道如何修复这个错误。
<?xml version="1.0"?>
<studentData>
<stuRec>
<as>
<sourceSys>BBC</sourceSys>
<acctDt>2023-04-04</acctDt>
</as>
<stats>
<ss>
<prov>AB</prov>
<cono>1</cono>
</ss>
</stats>
</stuRec>
<stuRec>
<as>
<sourceSys>RCD</sourceSys>
<acctDt>2023-05-14</acctDt>
</as>
<stats>
<ss>
<prov>ON</prov>
<cono>2</cono>
</ss>
</stats>
</stuRec>
</studentData>
awk -v element="stuRec" ' BEGIN { n =1; i = 1} /<\// && $0 ~ "</" element ">" { n++; } {print $0 > "output/part_" i ".xml"; if (n % 1 = 0) { i++; } }' "student_records.xml"
字符串
2条答案
按热度按时间vfwfrxfs1#
awk
是处理XML时使用的错误工具;你需要一些真正理解格式的东西,比如令人难以置信的方便的xmlstarlet
。一个示例脚本,首先使用它来查找文档中stuRec
节点的数量,然后循环,生成每个节点中具有给定数量的新XML文件:字符串
既然你说这是一个大文件,下面是一个perl脚本,它使用
XML::Twig
模块(使用你的操作系统的包管理器或最喜欢的CPAN客户端安装)来读取XML文档一次(并且不必在内存中保存整个解析的文档;它的目的是为了有效地处理非常大的XML文件):型
qnakjoqk2#
假设条件:
<stuRec>
之前的行(称为“头”)和最后一个</stuRec>
之后的行(称为“尾”)将被写入每个输出文件一个
awk
的想法,将 * 每个 *stuRec
块写入 * 单独的 * 文件:字符串
这产生:
型
注意事项:
X>1
数量的stuRec
块放入单个文件中,OP应该能够添加额外的计数器,并使用它来确定何时递增n
(从而创建新的输出文件)X>1
解决方案时遇到问题,那么我建议问一个新的问题,确保提供尝试的代码