Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Search and replace - command line solution (Read 5282 times)
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Search and replace - command line solution
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.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Search and replace - command line solution
Reply #1 - 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!
Back to top
 
« Last Edit: Mar 27th, 2009, 2:41pm by Mark Bernstein »  
WWW   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Search and replace - command line solution
Reply #2 - 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/(.+))
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print