Re: do shell script "perl... " to find/replace in a string
Re: do shell script "perl... " to find/replace in a string
- Subject: Re: do shell script "perl... " to find/replace in a string
- From: John Fowler <email@hidden>
- Date: Wed, 17 Mar 2004 16:41:36 -0600
I found success with the following handlers. Since the AplFolderString
variable is always a one-line string, the second handler called
perlreplace will work. The handler perlregexreplace meets the need
when the input string is multi-line. This is working in the context of
my AppleScript application but I can't say that I have comprehensively
tested this against all potential inputstrings, targetstrings, or
replacementstrings. Shell/perl can be funky (to an AppleScript guy,
perhaps not to a unix guy) with certain escape or control strings.
Hope this helps
John Fowler
on perlregexreplace(inputstring, targetstring, replacementstring)
set theFile to "Macintosh
HD:Users:johnfowler:Library:jwffolder:regexreplacetempfile.txt"
writeOutputFile(inputstring, theFile)
set theFile to convertFolder(theFile)
try
set shellscript to "/usr/bin/perl -0777e 'open (INFILE,\"" & theFile
& "\")||die (\"here\");$thisvar=<INFILE>;$thisvar=~s/" & targetstring &
"/" & replacementstring & "/sg;print $thisvar;'"
set theResult to (do shell script shellscript)
return theResult
on error thisError
return thisError
end try
end perlregexreplace
on convertFolder(AplFolderString)
set unixFolderString to
perlreplace(perlreplace((perlreplace(AplFolderString, "Macintosh HD:",
"/")), ":", "/"), " ", "\\ ")
return unixFolderString
end convertFolder
on perlreplace(inputstring, targetstring, replacementstring)
set shellscript to "/usr/bin/perl -e \"$rpl=" & quoted form of
replacementstring & ";$trgt=" & quoted form of targetstring &
";$thisvar=" & quoted form of inputstring &
";$thisvar=~s/$trgt/$rpl/sg;print $thisvar;\""
log shellscript
set theResult to (do shell script shellscript)
return theResult
end perlreplace
_______________________________________________
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.