in reply to Re: replacing text in specific tags
in thread replacing text in specific tags

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);

Replies are listed 'Best First'.
Re: replacing text in specific tags
by Abigail-II (Bishop) on Aug 21, 2003 at 10:35 UTC
    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

        Abigail-II is completely right about wasting time of others by not supplying the real example in the first place. Secondly, please use <code>-tags; they make code and input/output much more readable.
        Anyways, the following should work for you:
        #!perl use strict; use warnings; open(IN, "<temp.in") or die "Can't open file temp.in for input: $!\n" +; open(OUT,">temp.out") or die "Can't open file temp.out for output: $!\ +n"; for (<IN>) { s{<X-REF REFID="FN(\d+)">([^<]*)</X-REF>([^<]*)<FTN ID="FN\1">([^<] ++)</FTN>} {<X-REF REFID="FN$1">$2<FTN>$4</FTN></X-REF>$3}; print OUT; }
        You know, it would really, really help if you post your real problem the first time around. Now I've wasted time answering a question that isn't your problem at all.

        I'm not going to waste more time on you. One last hint: if you have exhausted your input in your first while loop, there isn't more input to read in the second loop.

        Goodbye.

        Abigail