regex - For each xml file in a directory, how to add its contents to another file at a certain place? -
i have fun , seemingly simple perl challenge stumping colleagues. care take crack? project move forward.
iterate through each xmlfilexf in directoryd, , each one:
- in modifythisfilemtf, copy wordblockwb , paste after last wordblockwb.
- in modifythisfilemtf, replace replacemerm contents of current xmlfilexf.
directoryd has xml files way, really, don't have discriminate on filetype. also, it's fine hardcode contents of wordblockwb if 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 help, , have fun!
this should job. prints stdout, redirect file needed.
#!/usr/bin/perl use strict; use warnings; $directoryd = "/xml files"; $prewords = "four score and...\n"; $postwords = "the end\n"; chdir("$directoryd") or die $!; opendir(d, ".") or die $!; @xmlfiles = grep(/\.xml$/i, readdir(d)); closedir(d); if (scalar(@xmlfiles) == 0) { die "could not detect xml files in $directoryd\n"; } print "myfunc\n"; print "{\n"; foreach $file (@xmlfiles) { # read first line each file. ignore other lines. open(f, "$file") or die $!; $line = <f>; chomp $line; close(f); print $prewords; print "\"msg\"=$line\n"; print $postwords; print "\n"; } print "return 0;\n"; print "}\n";
Comments
Post a Comment