• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: What is the most efficient/fastest Find & Replace technique?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the most efficient/fastest Find & Replace technique?


  • Subject: Re: What is the most efficient/fastest Find & Replace technique?
  • From: Philip Aker <email@hidden>
  • Date: Thu, 30 Jan 2003 15:46:37 -0800

On Thursday, January 30, 2003, at 10:47 AM, Terry Hill wrote:


A few years ago I broached this subject with Arthur Knapp and we basically decided that using Tanaka's MTReplace was the best option. However, since that time, I have/needed to migrate to OS X, but the osax has not, nor are there any plans to do so.

So, other than trying to figure out some very complex code, my best option seems to be to us Tex-Edit Plus's Replace command. Unless there is a better method of replacing several items, my question is:

Which is faster, call the Replace command one item at a time, or wrap it in a loop .... ?

Greetings Terry,

Depending on your needs, you might want to check out the Tcl methods for search and replace in strings described below. The situations where it is particularly good is if you are doing a lot of this stuff with large chunks of text. It can handle many advanced "grep" commands such as are used in BBEdit and many other editing applications. This code illustrates how to set up the "tclsh" environment to handle the generalized situation of one shot calls to Tcl and two example implementations of the Tcl "regsub" command which does all the work. If you wanted to speed that up, I can show you how to create a specialized shell script that only does regsub.

Otherwise, if you like OSAXEN, I think the Satimage one might have what you need.

Cheers,

Philip


-- Shell script to invoke tclsh directly. Must mark as executable and place in your search path.
-- Schnipp here. AppleScripts below.
#!/usr/bin/tclsh

if {$argc < 1 } {
set me [file tail $argv0]
puts stderr "Error: '$me' call has parameter(s) missing\nUsage: $me \"commands\""
exit 1
}

set result 0
foreach call $argv {
set result [catch {eval $call} errortext]
if {$result} {
puts stderr $errortext
} else {
puts stdout $errortext
}
}
exit $result



-- APPLESCRIPTS (put in separate files)

--#3
-- How to use Tcl's 'regsub' search and replace ignoring case
set Tcl_regsub to quoted form of "proc Tcl_regsub {theSearchPattern theString {theReplacePattern \"\"}} {
regsub -all -nocase -- $theSearchPattern $theString $theReplacePattern res;
puts -nonewline $res;
}
"
set res2 to <the code to get your sting here>
set fpat to "{([0-9A-Za-z_]+)}" --the search pattern
set rpat to "{Tcl_\\1} " --the replace pattern

set cmd3 to "Tcl_regsub " & fpat & " \"" & res2 & "\" " & rpat
do shell script "tcl " & Tcl_regsub & " " & quoted form of cmd3

--#4

-- Calling Tcl regsub with options. "-all" for find all occurences and "-nocase" for considering case
set Tcl_regsub to quoted form of "proc Tcl_regsub {searchPat inString replacePat {theOptions \"\"} } {
if {[string length $theOptions]} {
set str [string trim $theOptions]
if {[string compare -all $str] == 0} {
regsub -all -- $searchPat $inString $replacePat res;
puts -nonewline $res;
return;
} elseif {[string compare -nocase $str] == 0} {
regsub -nocase -- $searchPat $inString $replacePat res;
puts -nonewline $res;
return;
}
}
regsub -all -nocase -- $searchPat $inString $replacePat res;
puts -nonewline $res;
}
"

set res1 to <your string here>
set spat to "{([0-9A-Za-z_]+)}" -- the search pattern
set rpat to "{Tcl_\\1} " -- the replace pattern
set opts to "-all -nocase" -- the search options
-- -all means 'every occurrence', -nocase means "ignoring case" in AppleScript terms.
set cmd2 to "tcl " & Tcl_regsub & quoted form of (" Tcl_regsub " & spat & " \"" & res1 & "\" " & rpat)
set res2 to do shell script cmd2



Philip Aker
http://www.aker.ca
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: What is the most efficient/fastest Find & Replace technique?
      • From: David Hood <email@hidden>
References: 
 >What is the most efficient/fastest Find & Replace technique? (From: Terry Hill <email@hidden>)

  • Prev by Date: Re: Running AppleScript code from the Terminal?
  • Next by Date: Testing special characters (e.g., omega)
  • Previous by thread: Re: What is the most efficient/fastest Find & Replace technique?
  • Next by thread: Re: What is the most efficient/fastest Find & Replace technique?
  • Index(es):
    • Date
    • Thread