我有一个打印标题信息的awk脚本,然后遍历csv文件以创建xml输出。我希望在循环完成后打印一行,但是以下脚本将标记放在每行之后而不是仅放在文件末尾。我有什么不对?
BEGIN { FS="," }
NR==1 {
{print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > "output.xml" }
{print "<Batch>" > "output.xml" }
{print " <ASWFileVersion>" > "output.xml" }
{print " <Application>Batch Maintenance</Application>" > "output.xml" }
{print " <FileFormat>BATCH_XML_01</FileFormat>" > "output.xml" }
{print " <Release> </Release>" > "output.xml" }
{print " </ASWFileVersion>" > "output.xml" }
{print " <BatchHeader>" > "output.xml" }
{print " <BatchId>965</BatchId>" > "output.xml" }
{print " <UserBatchId>965</UserBatchId>" > "output.xml" }
{print " <BatchType>O</BatchType>" > "output.xml" }
{print " <Description>Brdata Customer Import</Description>" > "output.xml" }
{print " <CreatedOn>2014-12-05T12:35:30.3930000-05:00</CreatedOn>" > "output.xml" }
{print " <TargetData>BatchCustomer</TargetData>" > "output.xml" }
{print " <SaleLevel>0</SaleLevel>" > "output.xml" }
{print " <Status> </Status>" > "output.xml" }
{print " <ErrorMessage> </ErrorMessage>" > "output.xml" }
{print " </BatchHeader>" > "output.xml" }
for (i=1; i<=NF; i++) {
tags[i] = $i
}
next
}
{
print "<BatchCustomer>" > "output.xml"
for (i=1; i<=NF; i++) {
printf " <%s>%s</%s>\n", tags[i], $i, tags[i] > "output.xml"
}
print "</BatchCustomer>" > "output.xml"
}
{print "</Batch>" > "output.xml" }
我使用以下命令:
sudo gawk -f tst.awk customers.csv
运行该文件。输入文件是customers.csv,文件包含成为标记的标题。该脚本生成一个名为output.csv的文件。我需要标记仅位于文件的底部,而不是在从行生成的每个段的末尾。我做错了什么想法?
答案 0 :(得分:2)
你可能意味着
END {print "</Batch>" > "output.xml" }
其余的代码也非常糟糕,为什么要重定向每次写入而不是简单地重定向整个输出?