2

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?

  • The command in the applescript does not recognise the  . Also you use the -E option for extended RegEx. If you actually remove the last one you get the result. But I suspect you have them there for a reason. – markhunte Dec 7 '14 at 20:05
4

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; " & ¬
    "echo " & quoted form of original_text & " | sed -E 's|^(.*)\\$ ?(.) (.*)$|$ \\3|g'" as string
set formattedCode to do shell script commandString

Returns:

$ text1
$ text2
text3
$ text4

screenshot of solution 1

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 & 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 & ("$ " & 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

screenshot solution 2

  • Is not that simple. As input I receive several lines, ones can be preceded by the example ("string  string  string  $  text") but ones can't. Because of that I need the sed command working. If I change the sed option to -E works on the terminal, but not on the applescript. – jherran Dec 7 '14 at 20:17
  • @jherran, I updated my answer with what seems to be a working sed solution at least on my end. If that works for you too, I can clean up the answer removing the AppleScript attempts. – Ilari Scheinin Dec 7 '14 at 20:26
  • Very close @Ilari Scheinin, but unfortunatelly it only works for the last line. Check my question, I updated it a bit. – jherran Dec 7 '14 at 20:43
  • @jherran, I'm not sure what doesn't work: I get the exact same output running the command from the shell as I get running it from AppleScript (what you have listed under "Required output" except that there is an extra space before text3). So, I don't know if it's the regexp that needs tweaking, or something about the connection between AppleScript and the shell. (I'm on Yosemite 10.10.1 in case that could make a difference.) – Ilari Scheinin Dec 7 '14 at 20:51
  • For me on terminal works as described. From the applescript just display the last line. And, yes, I'm on yosemite. – jherran Dec 7 '14 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .