I am writing a script to insert 3 words into some xml files.

$dir contains many xml files with the same format.

The tag <dc:description> appears 4 times in each file

This script needs to go through the directory, and for each of the xml files, do nothing with the first <dc:description> tag, insert "Notes:" immediately after the 2nd tag, "Director:" after the 3rd tag, and "Actors:" after the 4rth tag.

I am having trouble with the code for inserting the 3 words. Here are a couple of things I tried at the ***insert***:
1.
s/(?:$search>){3}()\w+\s+/Notes:/; s/(?:$search>){5}()\w+\s+/Director:/; s/(?:$search>){7}()\w+\s+/Actors:/;

2.
Someone suggested below (I'm updating this post) using
my $data = do {local $/;<OLD>}; # slurp the whole file into $data my @replace_list = ('','Notes:','Director:','Actors:'); while ($data =~ s/(?<=$search)/shift @replace_list/egs) {};

(Thanks.) But I'm not familiar with some of this syntax and am not sure how to modify it

Here is my code. Can you show me either how to modify one of these methods or suggest another way to insert words after the repeated tags?

#!/usr/bin/perl -w use warnings; use strict; my $record_count = 0; my $search = 'dc:description'; my ($dir) = @ARGV; defined($dir) || usage(); chop($dir) if $dir =~ m#/$#; opendir(DIR, $dir) || die "Can't open $dir\n"; $file = readdir(DIR); $file = readdir(DIR); my $temp = 'temp.xml'; while (defined($file = readdir(DIR))) { print "Defined $dir/$file\n"; open(OLD, "< $dir/$file") or die "can't open $dir/$file: $!"; open(TEMP, ">> $dir/$temp") or die "can't open $dir/$temp: $!"; while (<OLD>) { #***insert*** print TEMP $_ or die "can't write $temp: $!"; } close(OLD) or die "can't close $dir/$file: $!"; close(TEMP) or die "can't close $dir/$temp: $!"; rename("$dir/$file", "$dir/$file.orig") or die "can't rename $file to +$file.orig: $!"; rename("$dir/$temp", "$dir/$file"); $record_count++; } closedir (DIR); msg("Processed $record_count files from $dir"); sub msg { print @_, "\n"; } sub usage { msg("Usage: $0 <directory>"); exit(1); }
Thanks!

In reply to Inserting Text into Files within a Directory by LF

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.