2016-04-12 50 views
1

を使用してファイルからmutiline文字列を交換してください使用:私は、それはperlの</p> <p>私がいたを使用して行うことができますどのように</p> <pre><code>String sql = Query1; </code></pre> <p>のようにそれを交換したい行</p> <pre><code>String sql="select * from "+ "emp_data"; </code></pre> <p>のように私が持っている私のファイルでのPerl

$curUrl ="select * from emp_data" ; 
$curUrl = quotemeta $curUrl; 
$newVariable = "Query1"; 
$data =~ s/$curUrl/$newVariable/g; 

hは単線で作業していました。壊れたものではない

+0

'/ gs'べき仕事 – rock321987

+0

それはJava言語ですか? –

答えて

0

これを行うには2つの代入を試みてください。

$curUrl ="select * from emp_data" ; 
$curUrl = quotemeta $curUrl; 
$newVariable = "Query1"; 
$data = 'String sql="select * from "+ 
     "emp_data";'; 

$data=~s/"|\+|\n\s+//g; # Here i replace the " + and \n\s character with empty. 

$data =~s/$curUrl/$newVariable/g; 
print $data;  
0

あなたは、これはsedスクリプトテスト使用を検討してください:

sed -n ' 
:1 
/^.*[+][ ]*$/ { 
    h; 
    :2 
    n; H; /^.*[+][ ]*$/ { 
    b2 
    } 
    x 
    s/[+\n\t\r]//g 
    s/["][[:space:][:blank:]]*["]//g 
    s/["][[:space:][:blank:]]*select [*] from emp_data[[:space:][:blank:]]*["]/Query1/ 
    p; x; 
    b1; 
}' 

テスト:

sed -n ' 
:1 
/^.*[+][ ]*$/ { 
    h; 
    :2 
    n; H; /^.*[+][ ]*$/ { 
    b2 
    } 
    x 
    s/[+\n\t\r]//g 
    s/["][[:space:][:blank:]]*["]//g 
    s/["][[:space:][:blank:]]*select [*] from emp_data[[:space:][:blank:]]*["]/Query1/ 
    p; x; 
    b1; 
}' myfile.txt 

String sql=Query1; 
0
use strict; 
use warnings; 
use 5.020; 
use autodie; 
use Data::Dumper; 

# Here, the variables $/, $^I, and @ARGV either have their default values 
# or some other values that were set by code appearing here. 

{ 
    local $/ = ";"; #local => temporarily change the value of this variable until the closing parenthesis of this block is encountered 
    local $^I = ".bak"; 
    local @ARGV = 'data.txt'; 

    while (<>) { 
     my $perl_statement = $_; 
     $perl_statement =~ s/sql = .*/sql = Query1/xms ; 
     print $perl_statement; #This is redirected into the file. 
    } 
} #Automatically restores the previous values for $/, $^I, and @ARGV. 

$/ =>入力行セパレータ(デフォルト=>「を\ n ")。 <$INFILE>は、1行で指定された文字までを読み込みます。
$^I =>文字列(デフォルト=> undef)に設定すると、ダイアモンド演算子が魔法のになり、一見ファイルを編集することができます。すべての印刷ステートメントは、新しいファイルに書き込まれます。名前は元のファイルと同じになります。 $^I = ".bak"と記述すると、元のファイルは元のファイル名に ".bak"という拡張子を加えたファイルに保存されます。空の文字列は、バックアップがないことを意味します。
@ARGV =>ダイヤモンド演算子は、この配列のファイルから読み取ります。

サンプル実行:

~/pperl_programs$ cat data.txt 
String sql="select * from "+ 
      "emp_data"; 
hello word="select * from "+ 
      "emp_data"; 

~/pperl_programs$ perl 1.pl 

~/pperl_programs$ cat data.txt 
String sql = Query1 
hello word="select * from "+ 
      "emp_data"; 

または、多分あなたは、パターンのすべての出現を置き換えたい:

use strict; 
use warnings; 
use 5.020; 
use autodie; 
use Data::Dumper; 

my $pattern = q{"select * from "+ 
      "emp_data"}; 

{ 
    local $/ = ";"; 
    local $^I = ""; 
    local @ARGV = 'data.txt'; 

    while (<>) { 
     my $perl_statement = $_; 
     $perl_statement =~ s/= \Q$pattern/ = Query1/xms; 
     print $perl_statement; 
    } 
} 

サンプル実行:

~/pperl_programs$ cat data.txt 
String sql="select * from "+ 
      "emp_data"; 
hello word="select * from "+ 
      "emp_data"; 

~/pperl_programs$ perl 1.pl 

~/pperl_programs$ cat data.txt 
String sql = Query1; 
hello word = Query1; 
関連する問題

 関連する問題