in reply to Search and Replace for XML

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

and you will get lots of help from Perlmonks.

Replies are listed 'Best First'.
Re^2: Search and Replace for XML
by srprabu (Initiate) on Apr 29, 2008 at 04:10 UTC

    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