Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Search and replace query

by heeru (Initiate)
on Aug 26, 2011 at 09:58 UTC ( #922557=perlquestion: print w/replies, xml ) Need Help??

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

Hi Experts, I have the following code (which parses through a file) looking for particular text.
$fileName = shift; open(HND, $fileName) or die "Can't open $fileName: $!"; @fileArr = <HND>; close(HND); foreach my $line ( @fileArr ) { $line =~ s/<bindpass>welcome<\/bindpass>/<bindpass>Welcome1<\/ +bindpass>/g; } open(HND, ">$fileName") or die "Cant open for writing $fileName: $!" +; print HND @fileArr; close(HND);
This works perfectly fine and replaces all occurrences in the file correctly. Issue here is, the third time this text appears, it appears after a line, lets say,  <bindname>abcde<\/bindname> . I do not want the occurrence to change in this case. So how can I include this in my code? Help appreciated. Thanks !

Replies are listed 'Best First'.
Re: Search and replace query
by moritz (Cardinal) on Aug 26, 2011 at 10:08 UTC
    You need to search for <bindname>abcde<\/bindname>, and if it appears in the current line, store that information in a variable.

    then if you do the substitution, check first if that variable is set.

    And to remember to reset that variable in an appropriate place

Re: Search and replace query
by cdarke (Prior) on Aug 26, 2011 at 10:23 UTC
    Is the condition the number of occurences "the third time this text appears" or when "it appears after a line, lets say, <bindname>abcde<\/bindname>"?

    Which condition you require will determine the type if if statement you require.
      foreach my $line ( @fileArr ) { next if $line =~ m/<bindname>abcde<\/bindname><bindpass>welcom +e<\/bindpass> $line =~ s/<bindpass>welcome<\/bindpass>/<bindpass>Welcome1<\/ +bindpass/ }
Re: Search and replace query
by RichardK (Parson) on Aug 26, 2011 at 11:58 UTC

    Your data looks like it's XML, so if it is you might find it easier to use an XML parser. A parser will handle any non significant whitespace or formatting changes & you can focus on only what's important to you.

    You could try XML::Simple to read the file into a hash and then modify the hash.

    Just a thought :) HTH

      Since the order of the keys is not preserved in a hash and the order of elements seems to be important here XML::Simple won't do the job. This would be easy with xpath.

      On second thought XML::Simple could work. You could check if the parent of this node contains a child with bindname=abcde.

        Since the order of the keys is not preserved in a hash and the order of elements seems to be important here XML::Simple won't do the job. This would be easy with xpath.

        XML::Simple knows how to array

Re: Search and replace query
by Anonymous Monk on Aug 26, 2011 at 13:08 UTC
    Two approaches using XML::Twig, too bad the xpath support is dated
    #!/usr/bin/perl -- use XML::Twig; BEGIN { *XML::Twig::Elt::x = *XML::Twig::Elt::get_xpath; } use strict; use warnings; my $str = <<'EOF'; <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>welcome</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> EOF { my $t = XML::Twig->new( pretty_print => 'indented', twig_handlers => { '//factory/drop' => sub { if ( not $_->x('bindname[string()=~/abcde/]') ) { if ( my ($pass) = $_->x('bindpass[string()="welcom +e"]') ) { $pass->set_text( uc $pass->text ); } } }, }, ); $t->parse($str); $t->print(); } { my $bozo = 0; my $t = XML::Twig->new( pretty_print => 'indented', twig_handlers => { '//factory/drop/bindname' => sub { $bozo = 1 if $_->text =~ /abcde/; }, '//factory/drop/bindpass' => sub { if ( not $bozo and $_->text =~ /welcome/ ) { $_->set_text( uc $_->text ); } $bozo = 0; }, }, ); $t->parse($str); $t->print(); } __END__
    And a similar one using XML::XSH2, too bad it doesn't have diagnostics/splain
    #!/usr/bin/perl -- use strict; use warnings; use XML::XSH2; xsh(<<'__XSH__'); open "fudge.xml"; ls --indent /; pwd ; cd /fudge/factory ; pwd ; foreach //drop { if not( bindname/text()[contains(.,'abcde')] ){ set bindpass xsh:subst( bindpass/text(), "welcome", "W-e-l-c-o-m-e", "i" ); }; }; ls --indent /; save --indent :b /; # overwrite original file, :b means backup __XSH__ __END__ $ perl xsh.922557.pl parsing fudge.xml done. <?xml version="1.0"?> <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>welcome</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> Found 1 node(s). / /fudge/factory <?xml version="1.0"?> <fudge> <factory> <drop> <bindname>qqqqq</bindname> <bindpass>W-e-l-c-o-m-e</bindpass> </drop> <drop> <bindname>abcde</bindname> <bindpass>welcome</bindpass> </drop> </factory> </fudge> Found 1 node(s). Document saved into file 'fudge.xml'. $ ls -loanh fudge* -rw-rw-rw- 1 0 252 2011-08-26 06:05 fudge.xml -rw-rw-rw- 1 0 234 2011-08-26 06:05 fudge.xml~

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://922557]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2019-10-24 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?