Re: How to use "do shell script" to search for RegExps
Re: How to use "do shell script" to search for RegExps
- Subject: Re: How to use "do shell script" to search for RegExps
- From: John Cochrane <email@hidden>
- Date: Thu, 22 Apr 2004 21:54:04 +1000
On 22/04/2004, at 06:56, Rebecca O'Connell wrote:
I have read a number of thread where someone suggests using "do shell
script" and perl to do a string search. Could someone post an example
of the
syntax one would use to do this? For example if wanted to do something
equivalent to
A previous thread for RegExps search and replace produced something
like the script below. You could adapt this to do what you wanted.
My apologies but I have forgotten who posted it originally
set theSource to "hello there" --or choose file of type "TEXT"
set theSearchPattern to "hello"
set theReplacePattern to "hi"
set theCaseFlag to false
RegExReplace(theSource, theSearchPattern, theReplacePattern,
theCaseFlag)
on RegExReplace(theSource, theSearchPattern, theReplacePattern,
theCaseFlag)
if class of theSource is not string then
set SourceFile to POSIX path of theSource
else
set SourceFile to "/tmp/RegEx"
set FileRef to open for access (SourceFile as POSIX file) with write
permission
set eof FileRef to 0
write theSource to FileRef
close access FileRef
end if
set theOptions to "gs"
if not theCaseFlag then set theOptions to theOptions & "i"
return do shell script "perl -e 'open(SourceFile,\"" & SourceFile &
"\");
while(<SourceFile>) {
$result = $_;
$result =~ s/" & theSearchPattern & "/" & theReplacePattern & "/" &
theOptions & ";
print $result;
}
close SourceFile;'"
end RegExReplace
_______________________________________________
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.