# Batch processing to search and replace in all posts

**URL:** https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621
**Category:** Development
**Tags:** rest-api
**Created:** [2015年九月22日 23:56 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621 "2015-09-22T23:56:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![dandv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dandv/32/169779_2.png) [@dandv](https://meta.discourse.org/u/dandv)
#### Post date: [2015年九月22日 23:56 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/1 "2015-09-22T23:56:03Z")

</div>

Is there a simple Ruby script somewhere that can be easily tweaked to do a search and replace in all posts?

I’ve [migrated from MyBB](https://meta.discourse.org/t/migrating-from-mybb/25563/5) and there are some tiny formatting tweaks that need to be made, e.g. replace `[hr]` with `----------`, or [convert the quote lines](https://github.com/discourse/discourse/pull/3802). I’m very new to Ruby (basically learned enough last night to submit that quoteline conversion PR), so I’m looking for a skeleton script that loops through existing posts.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年九月23日 00:13 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/2 "2015-09-23T00:13:27Z")

</div>

you can use `script/discourse remap` it may do the trick.

---

<div class="post-metadata">

### Author: ![dandv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dandv/32/169779_2.png) [@dandv](https://meta.discourse.org/u/dandv)
#### Post date: [2015年九月23日 01:23 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/3 "2015-09-23T01:23:02Z")

</div>

[remap](https://github.com/discourse/discourse/blob/master/script/discourse#L143) does a search and replace on text and char columns across all tables - a little rough of a tool. Also, how would the `raw` → `cooked` conversion be handled?

Is there a higher-level way of altering and rebaking posts, that would take into account things like re-computing excerpts, [quoted posts](https://github.com/discourse/discourse/blob/9ce66038647bc4ff63167fe9c74857a01acc0875/app/models/post.rb#L453) and [reply relationships](https://github.com/discourse/discourse/blob/9ce66038647bc4ff63167fe9c74857a01acc0875/app/models/post.rb#L469)? The latter are especially important when fixing the quote line after the import.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2015年九月23日 01:37 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/4 "2015-09-23T01:37:04Z")

</div>

You have a good point it might be nice to have a variant of this task that operates on just post bodies, @sam. It would be much safer too.

---

<div class="post-metadata">

### Author: ![dandv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dandv/32/169779_2.png) [@dandv](https://meta.discourse.org/u/dandv)
#### Post date: [2015年十月3日 01:20 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/5 "2015-10-03T01:20:57Z")

</div>

Authors of import plugins (e.g. @Sander78’s Facebook import) should be able to crank/extract one out relatively quickly.

Alternatively, I’ve released a Node.js script that uses the API to do search&replace:

> [@\[Announce\] Search & replace / batch process Discourse posts](https://meta.discourse.org/t/announce-search-replace-batch-process-discourse-posts/34029):
>
> ==\> [Discourse-post-process](https://github.com/dandv/discourse-post-process) \<== I’m happy to introduce this simple but powerful Node.js script that uses the [Discourse API](https://meta.discourse.org/t/discourse-api-documentation/22706) to mass-modify Discourse posts. It essentially performs a regexp-based search & replace across all posts in a forum. After migrating a few thousand posts from MyBB to Discourse, there was various little tweaks that needed to be made (fix BBCode → Markdown conversion, re-attach attachments, fix @username mentions when the usernames have changed etc.) I’m not a Ruby coder, and…

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2016年五月4日 23:20 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/6 "2016-05-04T23:20:20Z")

</div>

@techapj I would like a rake task that does string replacements scoped to just posts, if you can add that to your todo list. It can be a variant of the rake task that already exists, just a bit more specific in what it affects..

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [2016年八月17日 10:24 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/7 "2016-08-17T10:24:55Z")

</div>

> [@codinghorror](#):
>
> rake task that does string replacements scoped to just posts

Okay, we have one now:

> [@Replace a string in all posts](https://meta.discourse.org/t/remap-all-posts-matching-a-string/48729):
>
> bookmark This guide explains how to replace a string in all posts within a Discourse instance. person_raising_hand Required user level: System Administrator warning Console Access Required Want to replace a string in all the posts on a site? Let’s get started! warning WARNING: We strongly recommend you take a full backup before proceeding, and make sure your string replacement is specific enough to affect only the places you want it to. If this string replacement goes wrong, every…

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2019年二月23日 16:43 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/8 "2019-02-23T16:43:11Z")

</div>



---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2019年二月25日 03:58 UTC](https://meta.discourse.org/t/batch-processing-to-search-and-replace-in-all-posts/33621/9 "2019-02-25T03:58:04Z")

</div>


