試す:
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/\""
編集LauriRantaは、検索または置換文字列にエスケープされた二重引用符が含まれている場合、私の答えは機能しないという良い点を述べています。彼女の解決策は次のとおりです。
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 & "/"