2011-11-09 98 views
0
<?php 
class DATABASE_CONFIG { 
    public $default = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'hostname', 
     'login' => 'username', 
     'password' => 'password', 
     'database' => 'database_name', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 
    public $test = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'hostname', 
     'login' => 'username', 
     'password' => 'password', 
     'database' => 'database_name', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 
} 

登錄名,密碼和數據庫有一些默認值。我如何編寫腳本來編輯登錄名,密碼和數據庫?需要腳本來編輯文件

+0

你想改變$ default和$ test塊或者只是$ test嗎? – Kent

+0

是的,我想改變$ default和$ test。 – shibly

+0

查看我的更新回答 – Kent

回答

2

php文件包含您的示例文本:

kent$ awk '/login/{l++;if(l>1)gsub(/root/,"newLogin");} 
     /password/{p++;if(p>1)gsub(/xxx/,"newPwd");} 
     /database/{d++;if(d>1)gsub(/xxx/,"newDB");} 1' php 
<?php 
class DATABASE_CONFIG { 

    public $default = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'localhost', 
     'login' => 'root', 
     'password' => 'xxx', 
     'database' => 'xxx', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 

    public $test = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'localhost', 
     'login' => 'newLogin', 
     'password' => 'newPwd', 
     'database' => 'cake_test_db', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 
} 

您$默認塊之後這隻改變的塊。和,只有默認值將被改變。例如在你的$ test中,'database' => 'cake_test_db'的值不是默認的'xxx',所以它不會被改變。

如果你喜歡,你可以把新的價值放在變量中,並傳遞給awk與-v。

編輯,如果你想改變所有的值(包括$默認塊),它會更容易,檢查下面的線

:(同上,只defalut值(根,XXX,XXX )將被改變。

kent$ awk '/login/{gsub(/root/,"newLogin");}                       
      /password/{gsub(/xxx/,"newPwd");}                        
      /database/{gsub(/xxx/,"newDB");} 1' php                      
<?php 
class DATABASE_CONFIG { 

    public $default = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'localhost', 
     'login' => 'newLogin', 
     'password' => 'newPwd', 
     'database' => 'newDB', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 

    public $test = array(
     'datasource' => 'Database/Mysql', 
     'persistent' => false, 
     'host' => 'localhost', 
     'login' => 'newLogin', 
     'password' => 'newPwd', 
     'database' => 'cake_test_db', 
     'prefix' => '', 
     //'encoding' => 'utf8', 
    ); 
} 

EDIT2

這個工程的有關新的例子。

sed '/login/{s/username/newLOGIN/}; /password/{s/password/newPWD/2}; /database/{s/database_name/newDB/}' php 

    <?php 
    class DATABASE_CONFIG { 
     public $default = array(
      'datasource' => 'Database/Mysql', 
      'persistent' => false, 
      'host' => 'hostname', 
      'login' => 'newLOGIN', 
      'password' => 'newPWD', 
      'database' => 'newDB', 
      'prefix' => '', 
      //'encoding' => 'utf8', 
     ); 
     public $test = array(
      'datasource' => 'Database/Mysql', 
      'persistent' => false, 
      'host' => 'hostname', 
      'login' => 'newLOGIN', 
      'password' => 'newPWD', 
      'database' => 'newDB', 
      'prefix' => '', 
      //'encoding' => 'utf8', 
     ); 
    } 
+0

它只改變$ test塊,而不是$默認值。 – shibly

+0

檢查更新後的答案。 – Kent

+0

我已更改/編輯原始帖子/問題。 – shibly

1
#!/usr/bin/awk -f 

    BEGIN { FS="'" 

     usr[0] ="usr1" ; usr[1] = "user2" 
     pass[0] ="pass1" ; pass[1] = "pass2" 
      db[0] ="db1" ; db[1] = "db2" 
    } 
    { 
    if ($2 == "login") { 
     $0="\t'login' => '" usr[u] "'," 
     u++ 
    } 
    if ($2 == "password") { 
     $0="\t'password' => '" pass[p] "'," 
     p++ 
    } 
    if ($2 == "database") { 
     $0="\t'database' => '" db[d] "'," 
     d++ 
    } 
    print 
    }