AppleScript替换多个文件中的文本

时间:2018-10-12 19:42:44

标签: perl search text replace applescript

我正在尝试提出一种通用的通过AppleScript替换多个文件中的文本的多功能方法。该解决方案使用Perl。也许有更优雅的方法吗?

weighted_avg = ((old_wt * weighted_avg) +
                (new_wt * cur)) / (old_wt + new_wt)

此外,理想情况下,我想使Perl部分更具可读性,将每个搜索/替换模式放在单独的行中,但是AppleScript中的>>> from pandas._libs.window import ewma >>> %timeit ewma(s.values, 0.4, 0, 0, 0) 513 µs ± 10.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 似乎无法处理换行符,例如

set myFolder to choose folder with prompt "Choose a folder:"
tell application "Finder"
    try 
        set txtFiles to (every file in entire contents of myFolder whose name ends with ".txt") as alias list
    on error 
        try
            set txtFiles to ((every file in entire contents of myFolder whose name ends with ".txt") as alias) as list
        on error 
            set txtFiles to {}
        end try
    end try
    set myFiles to txtFiles
end tell

repeat with CurrentFile in myFiles
    set CurrentFile to CurrentFile as string

    do shell script "perl -pi -e  's/replace/me/g; s/andme/too/g;' " & quoted form of (POSIX path of CurrentFile)

end repeat

从本质上讲,有没有更好/更优雅的方法? 不一定必须与do shell script在一起,但是对于非专家,我继续找到do shell script "perl -pi -e ' s/replace/me/g; s/andme/too/g; s/andhere/measwell/g; ' " & quoted form of (POSIX path of CurrentFile) 来处理此问题的最简单方法,尤其是在正则表达式方面很出色(解决方案应该能够正则表达式)。

1 个答案:

答案 0 :(得分:2)

根据this,您可以使用

do shell script "perl -pi -e  '" & ¬
" s/replace/me/g;" & ¬
" s/andme/too/g;" & ¬
" s/andhere/measwell/g;" & ¬
"' " & quoted form of (POSIX path of CurrentFile)

请原谅我对AppleScript的了解,但是也许您可以执行以下操作:

set PerlProg to "" & ¬
"s/replace/me/g; " & ¬
"s/andme/too/g; " & ¬
"s/andhere/measwell/g;"

set PerlCmd to "perl -i -pe'" & PerlProg & "'"

do shell script PerlCmd & " " & quoted form of (POSIX path of CurrentFile)