in reply to Re: 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

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

Replies are listed 'Best First'.
with better example
by texuser74 (Monk) on Aug 21, 2003 at 11:04 UTC
    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; }
        Hi, Sorry for not providing a proper example at first time. Thank you very much for your kind support. I receive compilation errors while using your code, since i am a new user, i really don't know how to debug. but the following code works fine and i get my required output. just share with you i have pasted the code i am using now.
        open(IN, "<temp.in") || die "\n Can't open file\n $!\n"; open(OUT,">temp.out"); ## Search for FTN my %values; # read through the input fileto get the values while(<IN>) { if(/<FTN ID="FN(\d{3})">(.+?)<\/FTN>/) { #no need to quote <, >, + ", = $values{$1} = $2; } } close(IN); open(IN, "<temp.in") || die "\n Can't open file\n $!\n"; ## replace X-REF while(<IN> ) { s/<FTN.+?<\/FTN>//; # get rid of existing <FTN> tags - we alrea +dy have the values from them s/(<X-REF REFID="FN(\d{3})">.*)(<\/X-REF>)/$1<FTN>$values{$2}<\ +/FTN>$3/; # alter the X-REF LINE print OUT; } close(IN); close(OUT);
        Once again thanks for your support.

        edited by ybiC: Replace opening <pre> tag with <code> as per Monastery convention *and* add closing tag for same

      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