Support » Developing with WordPress » wp cli search-replace scripting

  • Resolved danhgilmore


    I have a bash script that runs a search-replace on every table in my WordPress installation. It works fine, but I would like to get more specific information of the changes.

    This is part of my bash script:

      wp --allow-root --url=$URL search-replace $OLD $NEW
    done < $SITE_LIST

    I used the –log option and it worked as expected, but the log was 30MB, and not easy to parse the data of what changed.

    Ideally, I would like to capture the following two bold lines from each wp search replace command as it’s run.

    wp –allow-root –url=$URL search-replace $OLD $NEW wp_1_options

    | Table        | Column       | Replacements | Type |
    | wp_1_options | option_name  | 0            | SQL  |
    <strong>| wp_1_options | option_value | 1            | PHP  |</strong>
    | wp_1_options | autoload     | 0            | SQL  |
    <strong>Success: Made 1 replacement.</strong>

    Is something like this possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • This should get you started:

    ./ | grep -E '(^\\| wp_(\d+_)?options\s+.*\soption_value|Made \d+ replacements.)'

    Thread Starter danhgilmore


    Thanks! I’ll use your regex and expand it to check for every table (not just options tables).

    Thread Starter danhgilmore


    Based on Mark’s example, I was able to tweak it to what I needed. This will work for every wp_* table in the DB, returning the tables changed or will be changed if using dry-run

    ./ | grep -E ‘(\\|\s[1-9]+|\d+ replace)’

    This was the return on one site:

    wp_37_comments comment_author_url 6 SQL
    wp_37_comments comment_content 3 SQL
    wp_37_options option_value 4 PHP
    Success: 13 replacements to be made.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp cli search-replace scripting’ is closed to new replies.