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',
);
}
你想改變$ default和$ test塊或者只是$ test嗎? – Kent
是的,我想改變$ default和$ test。 – shibly
查看我的更新回答 – Kent