2010-10-26 6 views
1

j'ai un fichier qui ressemble à:perl plusieurs lignes de substitution

<QUERY name="Secondary"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 
<QUERY name="primary"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 

<QUERY name="last"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 

je dois remplacer les SQLStmnts avec d'autres SQLStmnts et Nom de la requête que je recevais de commandline.

Comment puis-je utiliser une condition pour faire correspondre et substituer?

$qury_nm=shift; 
$sqlstmt=shift; 
undef $/; 
if(/<QUERY name="$qury_nm">(.*)<SQLStmt>(.*)<\SQLStmt>/) 
{ 
    #need help here!! 
    substitute the matched qury_nms SQLStmt wth the $sqlstmt and write it into the same file... 
} 
+0

http://codepad.org/ekwVuA5U pls voir ce lien pour une meilleure vue – Nachikethas

+0

Je ne pense pas que la commande « push » fait ce que vous pensez qu'il fait :-) –

+0

ooh ..il devrait être changement au lieu de pousser ... l'a changé :) – Nachikethas

Répondre

0

Voici un chemin à parcourir:

#!/usr/bin/perl 
use 5.10.1; 
use warnings; 
use strict; 

my $qury_nm = 'primary'; 
my $sqlstmt = 'SELECT col1,col2 FROM table2'; 
undef $/; 
my $str = <DATA>; 

$str =~ s!(<QUERY name="$qury_nm">.*?<SQLStmt>).*?(</SQLStmt>)!$1$sqlstmt$2!s; 
say $str; 

__DATA__ 
<QUERY name="Secondary"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 
<QUERY name="primary"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 

<QUERY name="last"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 

Sortie:

<QUERY name="Secondary"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 
<QUERY name="primary"> 
      <SQLStmt>SELECT col1,col2 FROM table2</SQLStmt> 

<QUERY name="last"> 
      <SQLStmt>select * from my_tb; 
      </SQLStmt> 
Questions connexes