对于目录中的每个xml文件,如何将其内容添加到某个位置的另一个文件中?

发布于 2024-11-06 04:24:24 字数 1007 浏览 0 评论 0原文

我有一个有趣且看似简单的 Perl 挑战,困扰着我的同事。介意破解吗?这将真正帮助我的项目取得进展。

迭代directoryD 中的每个xmlfileXF,并对每个文件:

  1. 在modifythisfileMTF 中,复制wordblockWB 并将其粘贴到最后一个wordblockWB 后面。
  2. 在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:

  1. in modifythisfileMTF, copy wordblockWB and paste it after the last wordblockWB.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音栖息无 2024-11-13 04:24:24

这应该可以完成工作。它打印到 STDOUT,因此只需根据需要重定向到文件即可。

#!/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";

This should do the job. It prints to STDOUT, so just redirect to a file as needed.

#!/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";
拍不死你 2024-11-13 04:24:24

这是打印到标准输出的另一种可能的解决方案:

#!/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";
}

它有点冗长,但其想法是将文件处理代码和格式化/输出代码分开。但在这种情况下,额外的复杂性可能不值得。

Here's another possible solution that prints to stdout:

#!/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";
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文