对于目录中的每个xml文件,如何将其内容添加到特定位置的另一个文件中?
我遇到了一个有趣且看似简单的Perl挑战,这让我的同事们感到困惑。要不要试试?这将真正有助于我的项目向前发展 迭代directoryD中的每个xmlfileXF,并针对每个xmlfileXF:对于目录中的每个xml文件,如何将其内容添加到特定位置的另一个文件中?,xml,regex,perl,scripting,Xml,Regex,Perl,Scripting,我遇到了一个有趣且看似简单的Perl挑战,这让我的同事们感到困惑。要不要试试?这将真正有助于我的项目向前发展 迭代directoryD中的每个xmlfileXF,并针对每个xmlfileXF: 在modifythisfileMTF中,复制wordblockWB并将其粘贴到最后一个wordblockWB之后 在modifythisfileMTF中,将replacemeRM替换为当前xmlfileXF的内容 顺便说一下,directoryD只有xml文件,所以实际上,您不必区分文件类型。此外,如果您
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
<foo/>
<bar/>
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
目录目录:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
<foo/>
<bar/>
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml内容:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
<foo/>
<bar/>
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
xmlfileXF2.xml内容:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
<foo/>
<bar/>
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
所需输出modifythisfileMTF.txt内容:
myfunc
{
// begin wordblockWB
prewords
"msg"=replacemeRM
postwords
// end wordblockWB
return 0;
}
xmlfileXF1.xml
xmlfileXF2.xml
...6000 more
<foo/>
<bar/>
myfunc
{
// begin wordblockWB
prewords
"msg"=<foo/>
postwords
// end wordblockWB
// begin wordblockWB
prewords
"msg"=<bar/>
postwords
// end wordblockWB
return 0;
}
myfunc
{
//开始wordblockWB
前置词
“味精”=
博文
//结束字块wb
//开始wordblockWB
前置词
“味精”=
博文
//结束字块wb
返回0;
}
谢谢你的帮助,祝你玩得开心 这应该可以完成这项工作。它会打印到标准输出,所以只要根据需要重定向到一个文件即可
#!/usr/bin/perl
use strict;
use warnings;
my $directoryD = "/xml files";
my $prewords = "Four score and...\n";
my $postwords = "The End\n";
chdir("$directoryD") or die $!;
opendir(D, ".") or die $!;
my @xmlFiles = grep(/\.xml$/i, readdir(D));
closedir(D);
if (scalar(@xmlFiles) == 0) {
die "Could not detect any XML files in $directoryD\n";
}
print "myfunc\n";
print "{\n";
foreach my $file (@xmlFiles) {
# Read the FIRST line from each file. Ignore any other lines.
open(F, "$file") or die $!;
my $line = <F>;
chomp $line;
close(F);
print $prewords;
print "\"msg\"=$line\n";
print $postwords;
print "\n";
}
print "return 0;\n";
print "}\n";
#/usr/bin/perl
严格使用;
使用警告;
my$directoryD=“/xml文件”;
my$prewords=“四分和…\n”;
my$postwords=“结束\n”;
chdir($directoryD)或die$!;
opendir(D,“.”)或die$!;
my@xmlFiles=grep(/\.xml$/i,readdir(D));
closedir(D);
if(标量(@xmlFiles)==0){
die“无法检测到$directoryD中的任何XML文件\n”;
}
打印“myfunc\n”;
打印“{\n”;
foreach my$文件(@xmlFiles){
#从每个文件中读取第一行。忽略任何其他行。
打开(F,“$文件”)或模具$!;
我的$line=;
chomp$行;
关闭(F);
打印$prewords;
打印“\”msg\“=$line\n”;
打印$postwords;
打印“\n”;
}
打印“返回0;\n”;
打印“}\n”;
以下是另一种可能的打印到标准输出的解决方案:
#!/usr/bin/env perl
use strict;
use warnings;
my $dir = shift || '.';
doit(sub {
my $callback = shift;
opendir(my $dh, $dir) or die $!;
while(my $file = readdir($dh)) {
next unless $file =~ /\.xml$/i;
open (my $fh, '<', $file) or die $!;
chomp(my $line = <$fh>);
close($fh) or die $!;
$callback->($line);
}
closedir($dh) or die $!;
});
sub doit {
my $process_files = shift;
print "myfunc\n{\n";
$process_files->(sub {
my $msg = shift;
print join "\n", map { "\t$_" } (
'// begin wordblockWB',
'prewords',
qq{"msg"=$msg},
'postwords',
'// end wordblockWB',
"\n"
);
});
print "\treturn 0;\n}\n";
}
#/usr/bin/env perl
严格使用;
使用警告;
我的$dir=shift | |';
多伊特(小组){
我的$callback=shift;
opendir(my$dh,$dir)或die$!;
while(my$file=readdir($dh)){
下一步除非$file=~/\.xml$/i;
打开(my$fh),所有XML文件是一行还是更长?如果更长,是否确实希望输出文件具有多行“msg”=…行?此外,输出文件可能是一种编程语言?您给出了确切的语法吗?没有行结尾分号等?xml文件都是一行,没有换行符,是的。我没有给出输出文件的确切编程语言语法;这不太重要。谢谢您的回答。如果您喜欢下面的答案之一,请参阅第页请不要忘记“接受”答案是正确的,所以作者得到了信任。很抱歉测试解决方案的延迟。我使用:/usr/bin/perl myscript>>myanswer.txt从Ubuntu计算机的命令行运行了这个命令,其中myscript是我存储上面代码的文件的全名。它工作起来很有魅力,对我这个perl新手来说很容易理解。This one也可以运行,但我必须将xml文件放在与脚本本身相同的目录级别,而且有点复杂。我自己是新手,不知道它是如何运行的,但它确实运行了。我用它来运行它:/usr/bin/env perl myscript>>myanswer.txt,其中myscript是我存储代码的文件。令我惊讶的是/usr/bin/env perl之所以有效,是因为我查看了/usr/bin/env,它似乎是一个别名和环境变量的列表,而我在我的特定系统上没有看到任何带有“perl”或“perl”的东西:/usr/bin/env|grep perl