dimanche 7 décembre 2014

Applescript to replace string using sed


I need and Applescript to replace some text as follow:


Original text:



string  string  string  $  text


The required output is:



$ text


I can do it in the terminal with this command:



$ echo "string  string  string  $  text" | sed -r 's/^(.*)\$ ?(.) (.*)$/$ \3/g'
$ text


By the way, i'm using bash version 4.3.30 and sed 4.2.2, both from homebrew.


The issue here is that I need to do it from an applescript. This is my approach:



set commandString to "echo \"string  string  string  $  text\" | sed -r 's|^(.*)\\$ ?(.) (.*)$|$ \\3|g'" as string
set formattedCode to do shell script commandString


And I get the following error:



error "sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]" number 1


If I remove the -r option, I get a different error:



sed: 1: "s|^(.*)\$ ?(.) (.*)$|$ ...": \3 not defined in the RE


If I remove the \3, the output must be $ instead of $ text, but sed command do nothing and it outputs:



string  string  string  $  text


I supposed that this might be a problem with sed version, am I right? How can I solve it?





Aucun commentaire:

Enregistrer un commentaire