【问题标题】:Replacing text with apostrophe text via sed in applescript通过applescript中的sed用撇号文本替换文本
【发布时间】:2012-10-09 03:25:37
【问题描述】:

我有一个苹果脚本来查找和替换一些字符串。前段时间我遇到了一个包含 & 的替换字符串的问题,但可以通过将 \& 放在替换属性列表中来解决它。然而,撇号似乎更烦人。

使用单个撇号会被忽略(替换不包含它),使用 \' 会产生语法错误(预期为“”,但发现未知标记。)并且使用 \' 会再次被忽略。(您可以继续这样做顺便说一句,偶数会被忽略,不均匀会导致语法错误)

我尝试将实际 sed 命令中的撇号替换为双引号(sed "s..." 而不是 sed 's...'),这在命令行中有效,但在脚本中出现语法错误(预期结束行等,但找到了标识符。)

单引号弄乱了shell,双引号弄乱了applescript。

我还按照here 的建议尝试了'\'' 和here 的'"'"'。

获取错误类型的基本脚本:

set findList to "Thats.nice"
set replaceList to "That's nice"
set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & fileName & " | sed 's/" & findList & "/" & replaceList & " /'"

【问题讨论】:

    标签: sed applescript replace


    【解决方案1】:

    试试:

    set findList to "Thats.nice"
    set replaceList to "That's nice"
    set fileName to "Thats.nice.whatever"
    set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/Thats.nice/That\\'s nice/\""
    

    或者坚持你的例子:

    set findList to "Thats.nice"
    set replaceList to "That's nice"
    
    set fileName to "Thats.nice.whatever"
    set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/" & findList & "/" & replaceList & "/\""
    

    解释:

    sed 语句通常用单引号括起来,如下所示:

    set myText to "Hello"
    set xxx to do shell script "echo " & quoted form of myText & " | sed 's/ello/i/'"
    

    但是,在本例中,您可以完全排除单引号。

    set myText to "Hello"
    set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i/"
    

    不加引号的 sed 语句将在包含空格后立即分解。

    set myText to "Hello"
    set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i there/"
    --> error "sed: 1: \"s/ello/i\": unterminated substitute in regular expression" number 1
    

    由于您不能在单引号语句中包含撇号(即使您将其转义),您可以将 sed 语句括在双引号中,如下所示:

    set myText to "Johns script"
    set xxx to do shell script "echo " & quoted form of myText & " | sed \"s/ns/n's/\""
    

    编辑 Lauri Ranta 提出了一个很好的观点,如果您的查找或替换字符串包含转义的双引号,我的答案将不起作用。她的解决方案如下:

    set findList to "John's"
    set replaceList to "\"Lauri's\""
    set fileName to "John's script"
    set resultFile to do shell script "echo " & quoted form of fileName & " | sed s/" & quoted form of findList & "/" & quoted form of replaceList & "/"
    

    【讨论】:

    • echo '\t' 在 bash 作为 sh 调用时打印文本选项卡,但 printf %s '\t'、shopt -u xpg_echo; echo '\t' 或 <<< '\t' 不会。可以使用"sed s/" & quoted form of findList & "/" & quoted form of replaceList & "/" 对模式进行外壳转义。
    • 嗨,劳里,我不确定我是否关注你。你能举一个我的答案中断的查找和替换示例吗?
    • findList 和 replaceList 不能在第二个代码块中包含双引号。 myText 不能包含像 \t 这样的转义序列。
    【解决方案2】:

    我也会使用文本项目分隔符。您不必将AppleScript's 包含在默认范围中,或者如果以后不使用该属性,则不必将其设置回来。

    set input to "aasearch"
    set text item delimiters to "search"
    set ti to text items of input
    set text item delimiters to "replace"
    ti as text
    

    如果模式包含可以被 sed 解释的内容,则没有简单的方法可以逃避搜索或替换模式。

    set input to "a[a"
    set search to "[a"
    set replace to "b"
    
    do shell script "sed s/" & quoted form of search & "/" & quoted form of replace & "/g <<< " & quoted form of input
    

    如果你必须使用正则表达式,像 Ruby 这样的脚本语言有从字符串创建模式的方法。

    set input to "aac"
    set search to "(a+)"
    set replace to "\\1b"
    
    do shell script "ruby -KUe 'print STDIN.read.chomp.gsub(Regexp.new(ARGV[0]), ARGV[1])' " & quoted form of search & " " & quoted form of replace & " <<< " & quoted form of input without altering line endings
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2019-09-06
      • 2023-03-02
      • 2013-10-19
      • 2013-08-09
      • 2014-12-22
      相关资源
      最近更新 更多