Applescript to replace string using sed 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?
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, add export 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):

set original_text to "string  string  string  $  text1
string  string  string  $  text2
text3
string  string  string  $  text4"

set commandString to "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; " &amp; ¬
    "echo " &amp; quoted form of original_text &amp; " | sed -E 's|^(.*)\\$ ?(.) (.*)$|$ \\3|g'" as string
set formattedCode to do shell script commandString


Returns:

$ text1
$ text2
text3
$ text4




Solution 2: AppleScript's text item delimiters

set original_text to "string  string  string  $  text1
string  string  string  $  text2
text3
string  string  string  $  text4"

set output to {}
set od to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"
"}
set all_lines to every text item of original_text
repeat with the_line in all_lines
    if "$" is not in the_line then
        set output to output &amp; the_line
    else
        set AppleScript's text item delimiters to {"$"}
        set latter_part to last text item of the_line
        set AppleScript's text item delimiters to {" "}
        set last_word to last text item of latter_part
        set output to output &amp; ("$ " &amp; last_word as string)
    end if
end repeat
set AppleScript's text item delimiters to {"
"}
set output to output as string
set AppleScript's text item delimiters to od
return output


Returns:

$ text1
$ text2
text3
$ text4