Jasmine Lognnes
Jasmine Lognnes

Reputation: 7097

Search and replace hash value. What am I doing wrong?

I have the following in the beginning of a file

<?php
return array(
    'DB' => array(
        'extTablesDefinitionScript' => 'extTables.php',
        'host' => '172.17.1.47',

where 172.17.1.47 can be anything. I would like to change whatever it is set to, to the content in $ip. So I have tried

ip="1.2.3.4"
sed -i "s/\'host\' => \'.*\',/$ip/g" /tmp/LocalConfiguration.php
sed -i "s/\(\'host\' => \'\).*/\1$ip\'/g" /tmp/LocalConfiguration.php

but is doesn't change anything.

Question

Can anyone see what is wrong? Or know a better way to do it?

Upvotes: 1

Views: 72

Answers (1)

anubhava
anubhava

Reputation: 785146

Quoting seems to be messed up. You can use:

ip="1.2.3.4"
sed -i.bak -r "s/('host' => ')[^']*'/\1$ip'/" file.php
cat file.php

<?php
return array(
    'DB' => array(
        'extTablesDefinitionScript' => 'extTables.php',
        'host' => '1.2.3.4',

Upvotes: 1

Related Questions