Shell 循环浏览要查找的列表(&a);代替

Shell 循环浏览要查找的列表(&a);代替,shell,sed,applescript,repeat,automator,Shell,Sed,Applescript,Repeat,Automator,在我的脚本中,字符串通常少于200个单词。 FindList和replaceWithList各有78个术语。。。查找第一个列表中出现的每个术语,并将其替换为第二个列表中的相应术语。 脚本运行良好,但在重复循环中,在78个不同的do shell脚本调用中执行sed命令的速度很慢。 如果将所有内容都传递给shell以便在那里进行迭代,则速度会更快。我该怎么做? 下面是applescript中的相关重复部分。我将把这个东西放到自动机中,这样我可以在“运行shell脚本”操作中做一些事情。我可以在一个由

在我的脚本中,字符串通常少于200个单词。 FindList和replaceWithList各有78个术语。。。查找第一个列表中出现的每个术语,并将其替换为第二个列表中的相应术语。 脚本运行良好,但在重复循环中,在78个不同的do shell脚本调用中执行sed命令的速度很慢。 如果将所有内容都传递给shell以便在那里进行迭代,则速度会更快。我该怎么做? 下面是applescript中的相关重复部分。我将把这个东西放到自动机中,这样我可以在“运行shell脚本”操作中做一些事情。我可以在一个由制表符分隔的数据字符串中找到和替换列表。查找和替换列表是常量,因此需要烘焙到shell脚本中的列表,并且只需要接收来自上一个操作的字符串

set theString to "foo 1.0 is better than foo 2.0. The fee 5 is the best."
set toFindList to {"foo", "fee", "fo", "fum"}
set replaceWith to {"bar", "bee", "bo", "bum"}
set cf to count of toFindList
-- replace each occurrence of the word followed by a space and a digit
repeat with n from 1 to cf
    set toFindThis to item n of toFindList
    set replaceWithThis to item n of replaceWithList
    set scriptText to "echo " & quoted form of theString & " | sed -e 's/" & toFindThis & " \\([0-9]\\)/" & replaceWithThis & " \\1/'g"
    set theString to do shell script scriptText
end repeat
return theString

好的,使用sed-f命令文件技术,我让它工作了。该脚本采用制表符分隔的字符串或文件,然后根据该字符串或文件构建sed命令文件

property theString: "foo 1.0 is better than foo 2.0. The fee 5 is the best."
property substitutionList : "foo    bar
fee bee
fo  bo
bum bum" -- this tab delim list will have 78 terms

set tabReplace to "\\( [0-9]\\)/"
set paragraphReplace to "\\1/g
s/"

-- parse the replace string into lists
set commandString to ""
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
set commandString to text items of substitutionList
set AppleScript's text item delimiters to tabReplace
set commandString to "s/" & commandString as string
set AppleScript's text item delimiters to return
set commandString to text items of commandString
set AppleScript's text item delimiters to paragraphReplace
set commandString to (commandString as string) & "\\1/g"
set AppleScript's text item delimiters to otid

set commandFilePath to ((path to temporary items from user domain) as text) & "commandFile.sed"
try
    set fileRef to open for access file commandFilePath with write permission
    set eof of fileRef to 0
    write commandString to fileRef
    close access fileRef
on error
    close access fileRef
end try
set posixPath to POSIX path of file commandFilePath

set scriptText to "echo " & quoted form of theString & " | sed -f " & quoted form of posixPath
set theString to do shell script scriptText
return theString

如果可以将搜索和替换字符串烘焙到shell脚本中,那么在单个sed脚本中也可以吗?这将消除循环和许多开始和结束执行上下文。如果可以,则将几行
s/foo/bar/g写入“fumbum.sed”;s/fee/bee/g;s/fo/bo/g.
并调用
sed-f fumbum.sed
。你能用
s/foo\([0-9])/bar\1/g这样的行创建一个命令文件并使用
sed-f commandfile
?@WalterA好的一点,我忘记了“\([0-9]\)”。是的,我可以将列表写入tmp文件。我从未将sed与命令文件一起使用过。顺便说一句,我也不必使用sed。