batch search and replace
Asked by Michael Roff:

Is there a way to batch search and replace within Confluence?

I have found an abandoned plugin which does this, but it does not support Confluence 3.4+

Is there a way to do this through webDav maybe? My attempts have failed at the point of accessing the Network Place for Confluence through various batch editors - all of which don't seem to be able to pickup the files in Confluence.

Any ideas / solutions would be appreciated.

Thanks - Mick


Answers:
author: Gavin Fowler
body: Hi Mick

I've not used CLI before and I'm sure that will service all your needs, however more of an FYI. This weekend i sucessfully used Jeans's resolve in a similiar question i posted. See: [https://answers.atlassian.com/questions/12526/confluence-wiki-markup-search-and-replace.](../12526/confluence-wiki-markup-search-and-replace. "Confluence Wiki Markup Search and Replace") this uses a plugin and regex queries. I sucessfully ran this against a confluence 3.5.11 instance to change wide range of user macros before upgrading to 4.0.

Gavin.

comments: author: Michael Roff
body: Gavin,

Thanks for the suggestion.

I looked at that plugin as a starting point but then steered clear simply because it was a bit hacky. With confirmation from you that you have successfully used it and everything is going well, I think I will give this a go on a trial basis to see if it will successfully work for me also.

Do you have any tips / tricks of things to try and avoid or suggested operations to make it easy to use?

I will be installing today, so hopefully I have it all worked out by this time tomorrow.

Thanks again. Mick


author: Gavin Fowler
body: Hi Mick

yeah I know what you mean about being a bit of a hack - i had exactly the same concerns. below is my my advice /experience when used against confluence 3.5.11 running on Ubuntu 10.4 LTS.

1. do all testing on non production environment and make sure you are 100% comfortable before running in a production environment.
2. Do look through the plugin wiki - it's a mess, but some key information is there (just scroll past the big exception at the top of the page)
3. It is a global search and replace within a space, spaces, all spaces. there is no sub selection within a space.
4. Validate that only what you want ot change is being changed.
5. It uses regex for the "search" and standard text in the replace  
   For example if you wanted to change macro "{mymacro}" to "{mynewmacro}" the search syntax would need to be escaped to "\\{mymacro\\}" the replace would simple be "{mynewmacro}"
6. I took Jeans advice and added in the backport jar. (download from here: <http://www.findjar.com/jar/mule/dependencies/maven2/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar.html>). I'm not sure how experienced you are - but i simply downloaded backport-util-concurrent-3.1.jar and the plugin jar and then injected the backport into the the plugin jar (global-search-replace-plugin-1.2.1.jar) - see code snippet below - and then used the plug-in manager to upload `global-search-replace-plugin-1.2.1.jar into confluence `  
7. One installed navigate to the url: {confluence base url}/plugins/servlet/searchReplace

```
$ mkdir ~/META-INF$ mkdir ~/META-INF/lib$ mv backport-util-concurrent-3.1.jar ~/META-INF/lib/$ jar uf global-search-replace-plugin-1.2.1.jar META-INF/lib/backport-util-concurrent-3.1.jar
```


author: Michael Roff
body: Thanks Gavin.

I am having some difficulties installing the plugin at the moment, but once I have it up and running, I will definitely be following your advice.


author: Michael Roff
body: Well the plugin is installed, but it's not working for us.

We are getting a "page not found" error when we navigate to the /plugins/servlet/searchReplace page.

It's not obvious what is causing the problem, all permissions look good and the plugin shows up as functional. Oh well. Luckily I have Bob's suggestion to fall back on - I'll be testing this today so fingers crossed it all works out.


author: Gavin Fowler
body: Hi Mick

that's a shame you cannot get this to work. I will use the email address you posted earlier (in Bob's thread) and attach the plugin jar file (with the back port embeded) that I uploaded and used against confluence 3.5.11. I'll do this in the next couple of hours once I'm at work.


author: Michael Roff
body: The modified plugin from Gavin worked a treat.

Thanks for all the help Gavin!


author: Arun Kumar
body: You can FIND and REPLACE multiple words in Multiple doc \& docx files using simple software  

http://www.technocomsolutions.com/advancefindreplacehelp.htm


author: Bob Swift (personal)
body: You can use [Confluence Command Line Interface](https://studio.plugins.atlassian.com/wiki/display/CSOAP) to script **getPageSource** and **storePage** after doing search and replace. If your use case is simple enough, you can use **findReplace** parameter on the **storePage** to do the replacement.

Example scripts are <https://bitbucket.org/bob_swift/confluence-cli/src/tip/src/main/resources/examples/replace.sql> and <https://bitbucket.org/bob_swift/confluence-cli/src/tip/src/main/resources/examples/replace.txt>

comments: author: Michael Roff
body: Thanks Bob. I have installed Confluence CLI and have just starting to play around with it.

The documentation is really good btw - great job!

Ideally, I would like to search and replace on all descendents under a specified page. If I wanted to replace the word HELP with Get Support could I do something like this?

confluence --action storePage --space "space name" --title "page name" --descendents --findReplace "Help:Get Support"

I have tried this out and it reports back successfully, but the page itself doesn't update. Does this script look right to you?


author: Michael Roff
body: Bob,

I have made some progress and have two command line scripts working.

**Add page contents to output file:**   
confluence --action getPageSource --space "Space Name" --title "Page Name" --file out.txt  

**Replace page with itself and run findReplace**   
confluence --action storePage --space "Space Name" --title "Page Name" --file out.txt --findReplace "Help:Get Support"

<br />

This is all working nicely. Now I am trying to insert these two command line scripts into a script file which will work through each page outputting its contents, then updating its contents and running --findReplace.

<br />

Is this the right direction do you think for a batch change?

Are there any examples of run scripts for Confluence CLI available?

Thanks


author: Bob Swift (personal)
body: I think you are on the right path. Here is an example I can add. You should be very careful to test this and consider using --simulate support and or limit your query to a test space. One key is to make sure your query only selects pages that have your replacement string so that only those pages get a new version with the updated text.

I can't add this as an attachment, so it might get messed up

# Run using something like  
# confluence --action runFromSql --file src/main/resources/examples/replace.sql --database confluence-3.5.0 --host imac.local --driver postgresql  
#  
# where replace.txt looks something like:  
# --action getPageSource --space @space@ --title "@title@" --file out.txt  
# --action storePage --space @space@ --title "@title@" --file out.txt --findReplace "Help:Get Support"  
#  

select  
'run' as action,  
'src/main/resources/examples/replace.txt' as file,  
'@space@:' \|\| s.spacekey \|\| ',@title@:' \|\| c.title as "findReplace"  
from content as c, bodycontent as b, spaces as s  
where  
c.contentid = b.contentid  
and s.spaceid = c.spaceid  
and c.content_status = 'current'  
and c.contentid = b.contentid  
and b.body like '%Help%'  
and c.contentid is not null  
and c.spaceid is not null


author: Bob Swift (personal)
body: Just saw your first comment now. **descendents** is not valid for getting and storing pages. However, if you adjust the query you can subset it to all descendents of a specific page using parentid etc....


author: Michael Roff
body: Thanks Bob.

Would you mind emailing me the attachment ... [mroff@hemispheregps.com](mailto:mroff@hemispheregps.com)

I am not sure I exactly follow yet, I will give it a go on a limited basis and see if I can get it to work.

It is obvious that the CLI is very powerful once you get it working and know how to command it. I am not sure I am there yet :(

Thanks for all the advice.


author: Bob Swift (personal)
body: I submitted the scripts and added links to them in the main answer.


author: Michael Roff
body: Oops, I missed that - thanks Bob.


author: Michael Roff
body: I got the scripts to work, but hit a snag. I am trying to replace a fairly complicated macro which includes multiple variables etc. The text includes such characters as , % : and in turn this confuses the scripting.

Likewise the replacement text also includes complicated text which includes , $ :

From the experiments I did, there isn't a way to replace text that includes these special characters due to the way the scripting works. Is that right Bob?

Is there a way to specify "This is the: exact text; that I want $$ to replace%?"


author: Bob Swift (personal)
body: See <https://studio.plugins.atlassian.com/wiki/display/CSOAP/Confluence+Command+Line+Interface?focusedCommentId=65668009#comment-65668009>


Tags: bulk-edit

replace