in reply to replacing text in specific tags

Well, the problem isn't very well defined. All you give is an example. So, I'm taking some liberties on the precise specification of how the "tags" may appear.

Assuming your text is stored in $_, you can do it in two lines. First line grabs the YYY info, second line substitutes the XXX. You may have to tweak the regexes depending on the precise specification.

my %data = m!<X-REF REFID="([^"]+)">([^<]+)</XREF>!g; s!<FTN ID="([^"]+)">[^<]+</FTN>!<FTN ID="$1">$data{$1}</FTN>!g;

Abigail

Replies are listed 'Best First'.
Re: Re: replacing text in specific tags
by texuser74 (Monk) on Aug 21, 2003 at 10:19 UTC
    That is not an example, it is the actual content which i have to change.

    now I am using the following code, but no output in temp.out.

    open(IN, "<$file_name_xml") || die "\n Can't open file\n $!\n"; open(OUT,">temp.out"); while(<IN>) { my %data = m#<X-REF REFID="([^"]+)">([^<]+)</XREF>#g; s#<FTN ID="([^"]+)">[^<]+</FTN>#<FTN ID="$1">$data{$1}</FTN>#g; print OUT $data; } close(IN); close(OUT);
      That is not an example, it is the actual content which i have to change

      Really? It's just "YYY" that you have to replace with "XXX"? Just those three time the letter "Y"? And your tags are always X-REF followed by exactly a space, then REFID, then a =, then a ", etc? No spaces around the =, no single quotes? Good, then my regexes don't need any work!

      print OUT $data;
      Why are you printing $data? What's in that?

      Furthermore, if you read in the file line by line, you will only do the substitution if both the X-REF and the corresponding FTN are on the same line.

      Abigail

        Hi, here is a better example, explaining what i want. INPUT

        This is to test<X-REF REFID="FN001">1</X-REF> some more text. <FTN ID="FN001">Example111</FTN>

        This is to test<X-REF REFID="FN002">2</X-REF> some more text. <FTN ID="FN002">Example222</FTN>

        This is to test<X-REF REFID="FN003">3</X-REF> some more text. <FTN ID="FN003">Example333</FTN>

        EXPECTED OUTPUT

        This is to test<X-REF REFID="FN001">1<FTN>Example111</FTN></X-REF> some more text.

        This is to test<X-REF REFID="FN002">2<FTN>Example222</FTN></X-REF> some more text.

        This is to test<X-REF REFID="FN003">3<FTN>Example333</FTN></X-REF> some more text.

        This is the full perl script now i am using

        open(IN, "<temp.in") || die "\n Can't open file\n $!\n";

        open(OUT,">temp.out");

        ## Search for FTN

        my %values;

        while(<IN>) {

        if(s/\<FTN ID\=\"FN(\d{3})\"\>(.+?)\<\/FTN\>/\<FTN$1\>/) {

        $values{$1} = $2;

        }

        }

        ## replace X-REF

        while(<IN>) {

        s/(<X-REF REFID="FN(\d{3})">)(.+?)(<\/XREF>)/$1$values{$2}$3/;

        print OUT;

        }

        close(IN);

        close(OUT);

        But i don't get any output in temp.out

        In my input file X-REF occurs first and FTN occurs next. So my script is not working properly