对于目录中的每个xml文件,如何将其内容添加到某个位置的另一个文件中?
我有一个有趣且看似简单的 Perl 挑战,困扰着我的同事。介意破解吗?这将真正帮助我的项目取得进展。
迭代directoryD 中的每个xmlfileXF,并对每个文件:
- 在modifythisfileMTF 中,复制wordblockWB 并将其粘贴到最后一个wordblockWB 后面。
- 在modifythisfileMTF中,将replacemeRM替换为当前xmlfileXF的内容。
顺便说一下,directoryD 只有 xml 文件,所以实际上,您不必区分文件类型。另外,如果您愿意,也可以对 wordblockWB 的内容进行硬编码。
修改这个文件MTF.txt 内容:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
directoryD 内容:
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
xmlfileXF1.xml 内容:
<foo/>
xmlfileXF2.xml 内容:
<bar/>
所需的输出 修改这个文件MTF.txt 内容:
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
感谢所有帮助,祝您玩得开心!
I have a fun and seemingly simple Perl challenge that is stumping my colleagues. Care to take a crack? It will really help my project move forward.
Iterate through each xmlfileXF in directoryD, and for each one:
- in modifythisfileMTF, copy wordblockWB and paste it after the last wordblockWB.
- in modifythisfileMTF, replace replacemeRM with the contents of the current xmlfileXF.
directoryD only has xml files by the way, so really, you don't have to discriminate on filetype. Also, it's fine to hardcode the contents of wordblockWB if you prefer.
modifythisfileMTF.txt contents:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
directoryD contents:
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
xmlfileXF1.xml contents:
<foo/>
xmlfileXF2.xml contents:
<bar/>
desired output modifythisfileMTF.txt contents:
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
Thanks for all help, and have fun!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以完成工作。它打印到 STDOUT,因此只需根据需要重定向到文件即可。
This should do the job. It prints to STDOUT, so just redirect to a file as needed.
这是打印到标准输出的另一种可能的解决方案:
它有点冗长,但其想法是将文件处理代码和格式化/输出代码分开。但在这种情况下,额外的复杂性可能不值得。
Here's another possible solution that prints to stdout:
It's a bit verbose but the idea was to keep the file processing code and formatting/output code separate. The extra complexity might not be worth it in this situation though.