Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Questions and Answers >> Search and replace - command line solution
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1238178222

Message started by Mark Anderson on Mar 27th, 2009, 2:23pm

Title: Search and replace - command line solution
Post by Mark Anderson on Mar 27th, 2009, 2:23pm

This is an answered question but one which might help others. In updating aTbRef I found I'd got all my image tags hard linked to the original aTbRef site's /images/ folder. I had several hundred $URL values like:

http://www.acrobatfaq.com/tbx/images/TBX_v4_5_0_About.jpg

...instead of:

http://www.acrobatfaq.com/atbref46/images/TBX_v4_5_0_About.jpg

...a pain to fix by hand.  Although TB has no search and replace tool, OS X's Unix command line tools offer a solution. I'm no CL expert - so this shows a solution like this isn't too hard to attempt. The tool I chose is 'sed'. First I tested a command line in Terminal (in your Applications/Utilities), knowing I didn't need to wory about spaces in my text or multiple instances of a match. My test was like so (I needed the forward slashes in case 'tbx' turned up in a file name):

echo aaa/tbx/bbb | sed 's/\/tbx\//\/atbref46\//'

Success, so I made a new TBX and tested this agent action:

$MyStr = runCommand("echo " + $URL + " | sed 's/\/tbx\//\/atbref46\//'");

I didn't use $URL on the left because as expected this didn't work first time - I didn't want to keep overwriting my starting text so used a different attribute for testing output. So, I got no output which I put down to insufficiently escaped forward slashes. Reading further on sed syntax I found I could use other characters than / to enclose my sed regular expression, like so:

$MyStr = runCommand("echo " + $URL + " | sed 's:/tbx/:/atbref46/:'");

Easier to read and it worked. Then it was just a matter of making an agent in aTbRef to find all $URL with '/tbx/ in the values and running this agent:

$URL = runCommand("echo " + $URL + " | sed 's:/tbx/:/atbref46/:'");

Job done, and next time it will be much easier.  Still, if taking this approach, I'd encourage testing on a simple new TBX until you're sure your code is working as expected. Note too this is a very simple regular expression replacement - most case may be more complex and invlove more testing for edge cases (unexpected matching).

I hope this helps anyone wondering if search and replace is possible without reverting to surgery on the XML source.

Title: Re: Search and replace - command line solution
Post by Mark Bernstein on Mar 27th, 2009, 2:40pm

Anaother way to do this, of course, is with an agent. Search for a regular expression in $URL

     Query: http:(.+)/tbx/images/(.+)

and reassemble the URL

     Action: $URL="http:"+$1+"/atbref64/images/"+$2

Again, test before using!

Title: Re: Search and replace - command line solution
Post by Mark Anderson on Mar 27th, 2009, 3:21pm

Much neater and quicker! I (re-)learn something every day.  For those new to this the actual query - as typed in the box - is:

URL(http:(.+)/tbx/images/(.+))

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.