Need help with bash script using sed

Discussion in 'Programming/Scripts' started by agentmoller001, Aug 15, 2016.

  1. I have been trying for hours to find and replace text in a file using sed for a script. I think the problem is a regex issue, but not sure.

    I am trying to replace the line below in a php file while in a ssh jail.
    Code:
    define('DB_HOST', 'localhost');
    Here's what I want to replace it with.
    Code:
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    define('DB_HOST', '127.0.0.1:3306');
    } else {
    define('DB_HOST', 'localhost'); }
    Someone told me it's easier to put the replacement code in a text file and add it using sed. Is that correct? If so, how?

    Below is what I've tried. Note: I added .* in case :3306 at the end of localhost. Could be wrong, too, but I have tried it without .* as well.
    Code:
    sed -i 's/define\(\'DB\_HOST\'\,\ \'localhost.*\'\)\;/if\ \(\ defined\(\ \'WP\_CLI\' \)\ \&\&\ WP\_CLI\ \)\ \{/ndefine\(\'DB\_HOST\'\,\ \'127\.0\.0\.1\:3306\'\)\;/n\}\ else\ \{/ndefine\ \(\'DB\_HOST\'\,\ \'localhost\'\)\;\ \}' wp-config.php
    When I run it from the command line (Debian Jessie), it displays >
    Code:
    [email protected]:/web$ sed -i 's/define\(\'DB\_HOST\'\,\ \'localhost.*\'\)\;/if\ \(\ defined\(\ \'WP\_CLI\' \)\ \&\&\ WP\_CLI\ \)\ \{/ndefine\(\'DB\_HOST\'\,\ \'127\.0\.0\.1\:3306\'\)\;/n\}\ else\ \{/ndefine\ \(\'DB\_HOST\'\,\ \'localhost\'\)\;\ \}' wp-config.php
    >
    
    What am I doing wrong? I've also tried with /r instead of /n, and added /g to the end.
    ---
     
    Last edited: Aug 15, 2016

Share This Page