我正在尝试在(g)AWK中创建一个脚本,在其中我要将以下EXACT行放在输出文本文件的开头:
<?xml version="1.0" encoding="UTF-8"?>
<notes version="1">
<labels>
<label id="0" color="30DBFF">Custom Label 1</label>
<label id="1" color="30FF97">Custom Label 2</label>
<label id="2" color="E1FF80">Custom Label 3</label>
<label id="3" color="FF9B30">Custom Label 4</label>
<label id="4" color="FF304E">Custom Label 5</label>
<label id="5" color="FF30D7">Custom Label 6</label>
<label id="6" color="303EFF">Custom Label 7</label>
<label id="7" color="1985FF">Custom Label 8</label>
</labels>
而这一点到最后:
</notes>
到目前为止,这是我的脚本:
BEGIN {printf("<?xml version="1.0" encoding="UTF-8"?>\n") > "notes.sasi89.xml"}
END {printf("</notes>") > "notes.sasi89.xml"}
我的问题是,它没有按照我想要的方式进行打印,而是在输出文件中显示了此信息:
<?xml version=1 encoding=-8?>
</notes>
有些字符和引号不见了,我曾尝试学习手册,但听起来对我来说太复杂了,如果有人愿意帮我或向正确的方向发展,我将不胜感激。
答案是Community Wiki,以给出应归还应得的学分。
您需要转义报价:
printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
我认为您的大纲脚本是一个反模式(实际上是两个反模式)。你有:
BEGIN {printf("<?xml version="1.0" encoding="UTF-8"?>\n") > "notes.sasi89.xml"}
END {printf("</notes>") > "notes.sasi89.xml"}
反模式是:
您重复文件名;你不应该。您最好使用:
BEGIN {file = "notes.sasi89.xml"
printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") > file}
END {printf("</notes>") > file}
首先,您不应该在awk
脚本中进行I / O重定向。您应该让外壳执行I / O重定向。
awk '
BEGIN {printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")}
END {printf("</notes>")}
' > notes.sasi89.xml
有时候脚本中的I / O重定向是适当的,但是那时候您需要输出到多个文件。如此处所示,很可能只有一个输出文件,则使脚本写入标准输出,然后让外壳程序进行I / O重定向。它要灵活得多。您可以更轻松地重命名该文件,并通过管道等将输出发送到其他程序,如果在awk
脚本中嵌入了输出文件名,则要困难得多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句