I need and Applescript to replace some text as follow:
Original text:
string string string $ text1
string string string $ text2
text3
string string string $ text4
The required output is:
$ text1
$ text2
text3
$ text4
I can do it in the terminal with this command:
$ echo "string string string $ text1
string string string $ text2
text3
string string string $ text4" | sed -r 's/^(.*)\$ ?(.) (.*)$/$ \3/g'
$ text1
$ text2
text3
$ text4
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. So, If I replace sed
with /usr/local/bin/sed
it does nothing again after line set formattedCode to do shell script commandString
.
Someone know where the problem is?
Best Answer
Solution 1: sed
Option -r of GNU sed is -E on the OS X/BSD sed (the one that comes with the OS,
/usr/bin/sed
). And to get rid of the encoding problem with 's, addexport LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8;
to the beginning of the do shell script command (see the question here):Returns:
Solution 2: AppleScript's text item delimiters
Returns: