2012-02-16 75 views
0

我試圖用perl更新配置文件,我想出了一種方法來讀取文件並更新鍵值對。但是,我想通過前面加上#匹配,然後將字符串中的以下Perl腳本,用#前綴字符串匹配,在下面插入新字符串

行現有的腳本來改善它:

local($^I, @ARGV) = ("sourcefile"); 
    while (<>) { 
      s,^key1=.*$,key1=value1,; 
    s,^key2=.*$,key2=value2,; 
    s,^key3=.*$,key3=value3,; 
      print; 
      close ARGV if eof; 
    } 

期望的結果:

file before: 

    key1=value1 
    key2=value2 
    key3=value3 


file after: 

    key1=value1 
    #key2=value2 
    key2=test 
    key3=value3 

回答

3
if (/match/) { 
    print "#$_"; 
    s/.../.../; 
    print 
}