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

hi, i am trying to replace the following line in around 100 xml files :-
<value>javascript:var x=window.op +en('http://v3vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109&amp; +IDENT=US-x&amp;Target=General&amp;Country=Singapore','_blank','menuba +r=no,location=no,resizable=yes,scrollbars=no,status=no');</value> + </item> "
I want to replace IDENT=US-x with IDENT=UK-x in all files ,only the value x is different in al files. I am trying to use the following code for it but I am not able to do it :-
#!/usr/bin/perl use strict; use File::Find; use Tie::File; my $input="C://data"; my $logfile="C:\log.txt"; find(\&filenames, $input); sub filenames() { if (-f $File::Find::name) { my $file=$File::Find::name; if($file=~m/\/data\//) { open(FILE, "$file"); my @filedata=<FILE>; close(FILE); my $datalength = scalar(@filedata); my $DCRContent=""; for (my $i=0;$i<$datalength; $i++) { if($filedata[$i]=~/(.*)(http:\/\/v3vi.click4assistance +.co.uk\/c4a.aspx\?AccNo=Inter12109&amp;IDENT=)(.*)(','_blank)(.*)/i) { print "$3 \n"; my $orig=$3; print "$orig \n"; $orig=~ s/US/UK/; print "The resulting value is : $orig \n"; $filedata[$i] = $1.$2."$orig".$4.$5; open(FILEOUTS,">>$logfile") or die("Opening file failed"); print FILEOUTS $file."\t"."\n"; close(FILEOUTS); } } open(FILEOUT,">$file") or die("Opening file failed"); print FILEOUT @filedata; close FILEOUT; } } }
basically i am able to replace us with UK and store it in $orig variable but with the next line "$filedata$i = $1.$2."$orig".$4.$5; " the entire line gets deleted except "IDENT=US-x&Target=General&Country=Singapore'" ,so how to really get the combined output ,i am bit new to perl so may be i am making a simple mistake ,i request the experts to pls guide regarding this issue , thanks in advance

Replies are listed 'Best First'.
Re: regex issue
by JavaFan (Canon) on Dec 11, 2009 at 15:26 UTC
    I would something like:
    foreach my $line (@filedata) { s{(\Qhttp://v3vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109& +amp;IDENT=\E)US/${1}UK/g; }

      Doesn't the regex substitution pattern have two parts...the part to be look for and the part to use for the replacement?

      I like your solution, but I'm confused by the use of braces (i.e., { and }) for the first part and then it appears you're using the slash, /, for the second part.

      Did you mean:

      s{(\Qhttp://v2vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109&amp;IDENT=\E)US}${1}US}/g

      If so, then how can you use the braces to separate the part of the substitution and also for the numeric placeholder? It seems like it should be something like this:

      s|(\Qhttp://v3vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109&amp; +IDENT=\E)US|${1}UK|g;

      so that the regex separators are distinct from the characters (like the braces) and don't get confused with the separators.

      But you're a lot more experienced than I, so I'm sure I'm missing something...something that's probably obvious. As I noted, I really like your regex...even though I don't entirely understand it...so I'm anxious to learn more.

      Thanks, JavaFan, I always learn from eveyone here.

      ack Albuquerque, NM
        Yes, I should have used {} all the way:
        s{(\Qhttp://v2vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109&amp; +IDENT=\E)US}{${1}UK}g
        Note that both the pattern and the replacement have their own set of parens. You may write:
        s{(\Qhttp://v2vi.click4assistance.co.uk/c4a.aspx?AccNo=Inter12109&amp; +IDENT=\E)US} {${1}UK}g
        as well.
Re: regex issue
by Anonymous Monk on Dec 11, 2009 at 15:45 UTC
    #!/usr/bin/perl -- use strict; use warnings; use URI(); use CGI(); use URI::QueryParam(); $_ = <<'__STRING__'; <value>javascript:var x=window.open('http://v3vi.click4assistance.co.u +k/c4a.aspx?AccNo=Inter12109&amp;IDENT=US-x&amp;Target=General&amp;Cou +ntry=Singapore','_blank','menubar=no,location=no,resizable=yes,scroll +bars=no,status=no');</value></item> __STRING__ print "$_\n"; if( /^(.*?window.open\(')([^']+)('.*)/mg ){ my( $pre, $uri, $post ) = ( $1, $2, $3 ); $uri = URI->new( CGI->unescapeHTML( $uri) ); $uri->query_param(IDENT => 'UK-x'); $uri->query_param(Country => 'Cheddar'); print $pre, CGI->escapeHTML($uri),$post,"\n"; } __END__ <value>javascript:var x=window.open('http://v3vi.click4assistance.co.u +k/c4a.aspx?AccNo=Inter12109&amp;IDENT=US-x&amp;Target=General&amp;Cou +ntry=Singapore','_blank','menubar=no,location=no,resizable=yes,scroll +bars=no,status=no');</value></item> <value>javascript:var x=window.open('http://v3vi.click4assistance.co.u +k/c4a.aspx?AccNo=Inter12109&amp;IDENT=UK-x&amp;Target=General&amp;Cou +ntry=Cheddar','_blank','menubar=no,location=no,resizable=yes,scrollba +rs=no,status=no');</value></item>
      Hi, thanks for so much support and code given , regards, rahul