Arthor
Arthor

Reputation: 674

sed replace double quoted text, single quoted working

I am using SED to replace some key words in the Wordpress config file (setup-config.php).

The code located below is what I am using the change the keyworkds

sed -i -e "s/( 'wordpress'/( '$varname'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"
sed -i -e "s/( 'username'/( '$varname'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"
sed -i -e "s/( 'password'/( '$autogenpass'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"
sed -i -e "s/( 'wp_'/( '$autogenprefix'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"

Below are the 2 lines which work which use ' (single quotes)

<?php echo htmlspecialchars( _x( 'username', 'example username' ), ENT_QUOTES ); ?>
<?php echo htmlspecialchars( _x( 'password', 'example password' ), ENT_QUOTES ); ?>" autocomplete="off" />

Below are the 2 lines which DO NOT work which use " (double quotes)

<td><input name="dbname" id="dbname" type="text" aria-describedby="dbname-desc" size="25" value="wordpress"<?php echo $autofocus; ?>/></td>
<td><input name="prefix" id="prefix" type="text" aria-describedby="prefix-desc" value="wp_" size="25" /></td>

When replacing words with double quotes around them, it does not do it "wordpress" and "wp_".

I tried the following:

sed -i -e "s/( '"wordpress"'/( '$varname'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"
sed -i -e "s/( "'"wordpress"'"/( '$varname'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"
sed -i -e "s/( '""wordpress""'/( '$varname'/g" "/var/www/$domainname/html/wp-admin/setup-config.php"

I am not sure what I am missing here.

Thanks

Upvotes: 0

Views: 85

Answers (1)

Barmar
Barmar

Reputation: 782624

The easiest way is to define a variable that contains a double quote, then you can use that inside the string.

q='"'
sed -i -e "s/${q}wordpress${q}/${q}$varname${q}/g" "/var/www/$domainname/html/wp-admin/setup-config.php"

Or you can escape the double quotes:

q='"'
sed -i -e "s/\"wordpress\"/\"$varname\"/g" "/var/www/$domainname/html/wp-admin/setup-config.php"

Either way, you shouldn't have any single quotes in the regexp.

Upvotes: 2

Related Questions