Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>php file contains your example text:</p> <pre><code>kent$ awk '/login/{l++;if(l&gt;1)gsub(/root/,"newLogin");} /password/{p++;if(p&gt;1)gsub(/xxx/,"newPwd");} /database/{d++;if(d&gt;1)gsub(/xxx/,"newDB");} 1' php &lt;?php class DATABASE_CONFIG { public $default = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'localhost', 'login' =&gt; 'root', 'password' =&gt; 'xxx', 'database' =&gt; 'xxx', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); public $test = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'localhost', 'login' =&gt; 'newLogin', 'password' =&gt; 'newPwd', 'database' =&gt; 'cake_test_db', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); } </code></pre> <p>this only changes the blocks after your $default block. and, <strong>only</strong> default value will be changed. e.g. in your $test, <code>'database' =&gt; 'cake_test_db'</code> the value is not the default 'xxx', so it would not be changed.</p> <p>if you like you can put new value in variable, and passing to awk with -v.</p> <p><strong>edit</strong></p> <p>if you want to change all values, (including $default block), it would be easier, check the lines below: (same as above, only defalut values (root, xxx,xxx) would be changed.</p> <pre><code>kent$ awk '/login/{gsub(/root/,"newLogin");} /password/{gsub(/xxx/,"newPwd");} /database/{gsub(/xxx/,"newDB");} 1' php &lt;?php class DATABASE_CONFIG { public $default = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'localhost', 'login' =&gt; 'newLogin', 'password' =&gt; 'newPwd', 'database' =&gt; 'newDB', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); public $test = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'localhost', 'login' =&gt; 'newLogin', 'password' =&gt; 'newPwd', 'database' =&gt; 'cake_test_db', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); } </code></pre> <p><strong>edit2</strong></p> <p>this works for your new example in question.</p> <pre><code> sed '/login/{s/username/newLOGIN/}; /password/{s/password/newPWD/2}; /database/{s/database_name/newDB/}' php &lt;?php class DATABASE_CONFIG { public $default = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'hostname', 'login' =&gt; 'newLOGIN', 'password' =&gt; 'newPWD', 'database' =&gt; 'newDB', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); public $test = array( 'datasource' =&gt; 'Database/Mysql', 'persistent' =&gt; false, 'host' =&gt; 'hostname', 'login' =&gt; 'newLOGIN', 'password' =&gt; 'newPWD', 'database' =&gt; 'newDB', 'prefix' =&gt; '', //'encoding' =&gt; 'utf8', ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload