srprabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am new to perl, i am in need of a search and replace to be happen between two files, ie., search has to be happen in a file let it be, a.txt and replace to be happened in another file b.txt

For Example

In a.txt, file structure will be as follows
<author>
<afiil>
etc...

In b.txt, file structure will be as follows
<author>This is the sample author </author><afiil> This is the sample affiliation</afiil>

Now my replace should be happen like, perl has find the <author> from a.txt and that has to replace in a newline in b.txt same way <afiil> has to be replaced.

and one more question, please

i want to delete text between the tag <! ... >

Similarly between &! ... ;

How can i check the open tag and close tag, there may be many closing tag similarly semicolon will come in the between the open tag(<! or &!) and close tag (> or ;).

Regards,

srprabu

Replies are listed 'Best First'.
Re: Search and Replace for XML
by Narveson (Chaplain) on Apr 28, 2008 at 13:36 UTC

    Rather than reinventing the wheel, use one of the many available CPAN modules that know how to parse and generate (read and write) XML. Look in the XML namespace on CPAN.

    Start by writing a small program to locate the author and afiil nodes in a stripped-down version of your a.txt and print what it finds. If you run into difficulties, show us

    • your small XML input file
    • what information you want to output
    • the program you wrote
    • the output you actually got

    and you will get lots of help from Perlmonks.

      Hai Narveson,

      Thank you for considering me, let me tell you straight, There are two file a.txt and b.txt, i want each line of text in a.txt should be searched, if searched text found in b.txt then it has to replace with a newline along with the searched text, i hope so this will be clear to you.

      Regards,

      Sriram

        How about you show us a very small sample data set and the code you are having trouble with along with the output you get and the output you expect.

        Note that for small samples it is convenient to use strings as the input files and to print output. Consider:

        use strict; use warnings; my $fileA = <<FILEA; Thank you for considering me. Let me tell you straight, there are two file a.txt and b.txt. I want each line of text in a.txt should be searched FILEA my $fileB = <<FILEB; txt FILEB my @words; open my $bIn, '<', \$fileB or die "Open fileB failed: $!"; while (<$bIn>) { chomp; push @words, $_; } close $bIn; my $match = join '|', @words; open my $aIn, '<', \$fileA or die "Open fileA failed: $!"; while (<$aIn>) { next unless /\b($match)\b/; print; } close $aIn;

        Prints:

        Let me tell you straight, there are two file a.txt and b.txt. I want each line of text in a.txt should be searched

        which reads a "file" containing a list of words - one per line, then prints lines from a second "file" that contain any word from the list. Not the job you want to do I know, but I can't yet tell what it is that you do want to do!


        Perl is environmentally friendly - it saves trees

        Hai Narveson,

        Sorry Narveson, as i am new to this forum by absently i posted my reply in the comments, i am extremely sorry!

        Thank you for considering me, let me tell you straight, There are two file a.txt and b.txt, i want each line of text in a.txt should be searched, if searched text found in b.txt then it has to replace with a newline along with the searched text, i hope so this will be clear to you.

        Regards,

        srprabu
Re: Search and Replace for XML
by Utilitarian (Vicar) on Apr 28, 2008 at 12:19 UTC
    Could you clarify the question a bit.

    Do you want the content of b.txt to be entered into the xml of a.txt, under what conditions?

    To delete the content of the <!..> tag

    $line=~s/<![^>]*>/<!>/;
    

      Consider:

      my $line = '<!-- Gone > Not gone -->'; $line =~s/<![^>]*>/<!>/; print $line;

      Prints:

      <!> Not gone -->

      which most likely is not the expected result.


      Perl is environmentally friendly - it saves trees

        Hai,

        Thank you for considering me, let me tell you straight, There are two file a.txt and b.txt, i want each line of text in a.txt should be searched, if searched text found in b.txt then it has to replace with a newline along with the searched text, i hope so this will be clear to you.

        The code which you gave for replace is working only if the text come in between the open tag and closed tag,

        For example

        <! This is the sample text > -- For this the search is working fine.

        <! This is the <i>sample</i> text > -- For this the search is working till the italic closing tag not the actual closing tag.

        Regards,

        Srprabu