天天看點

perl中替換文本一例

由于工作需要,需要批量建立網站,修改配置檔案。開始時用sed去批量修改的,最後改裝成perl了。

#!/usr/bin/perl

while(<>){

chomp;

@line=split(/\s+/,$_);

open FH,"< /home/test/$line[0]/wp-config.php";

my @webfile;

while(<FH>){

        s/(ME|ER)', 'root/$1', '$line[1]/g;

        push @webfile,$_;

}

close FH;

open FH,"> /home/test/$line[0]/wp-config.php";

print FH for @webfile; #相當于foreach $mm (@webfile){

                               #     print FH $mm;        

                                #   

                                #       }

輸入的檔案

域名      資料庫名稱

a1.com       a1_db

a2.com        a2_db