Home > Linux > Search and replace MySQL content

Search and replace MySQL content

January 6th, 2010 Leave a comment Go to comments

Today I switched from the Code Markup to the Code Snippet WordPress plugin. Therefore I had to change the way the code block is activated. This is a time consuming job if you have to do this by hand, so I figured out there should be a smarter way of doing this. Check out the MySQL replace function below:

update [table_name] set [field_name] = replace([field_name],’[string_to_find]‘,’[string_to_replace]‘);

And so the above translated to the lines I needed:

mysql> update wp_vleeuwen_posts set post_content = replace(post_content, ‘<pre><code>’, ‘[code lang="text"]‘);
Query OK, 35 rows affected (0.06 sec)
Rows matched: 72  Changed: 35  Warnings: 0

mysql> update wp_vleeuwen_posts set post_content = replace(post_content, ‘</code></pre>’, ‘[/ code]‘);
Query OK, 35 rows affected (0.01 sec)
Rows matched: 72  Changed: 35  Warnings: 0

mysql>

Notice the little typo I had to make in order to let the above code snippet work ([/ code]).

Categories: Linux Tags: ,
  1. No comments yet.
  1. No trackbacks yet.