• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: sed replacement headscratcher
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed replacement headscratcher


  • Subject: Re: sed replacement headscratcher
  • From: Christopher Nebel <email@hidden>
  • Date: Sat, 6 Mar 2004 21:09:49 -0800

On Mar 6, 2004, at 4:02 AM, Walter Ian Kaye wrote:

At 12:23a -0700 03/06/2004, Gnarlodious didst inscribe upon an electronic papyrus:

set someChars to " & ' "

--when I say
set someChars to do shell script "echo " & the quoted form of someChars & "|
sed " & quoted form of "s|&|\\\\&amp;|"
-- I get the desired substitution -> \&amp;

-- but
set someChars to do shell script "echo " & the quoted form of someChars & "|
sed " & quoted form of "s|'|\\\\&#139;|"
-- gets me -> \'#139;

???

You have the wrong number of backslashes. If you test in Terminal, you'll see that you need three, which means you need six in AS.

::setting up command::

Agh! 'do shell script' is mangling it!!!

I did a 'set cmd to <blahblah>', then 'set the clipboard to cmd', pasted to the Terminal, and it worked. Then I did 'do shell script cmd' and it gave the wrong result, as if the number of backslashes had changed!

So... bad news, Rache -- 'do shell script' appears to be broken.
(I guess that's bad news for all of us.)

"do shell script" is neither broken nor inconsistent -- you're looking at the wrong shell. Try pasting it into sh instead of csh, and you'll get consistent results. sh and csh use different rules inside double-quoted strings, and sh's are a bit subtle. Walter is correct that sed needs three backslashes to get the desired effect; the problem is how many slashes do you have to put so the shell turns them into three?

In csh, backslashes inside double quotes are interpreted literally, so you need three backslashes (or six in AppleScript). In sh (which, of course, is what "do shell script" always uses and may very well NOT be your default shell), backslash inside double-quoted strings is a meta-character, but only when followed by $, `, ", \, or newline. Therefore, the correct number of slashes is five: the first four come out as "\\", which sed interprets as one backslash; the fifth is followed by &, so it gets passed literally, and sed sees \&, which suppresses the normal meaning of "&" in the replacement pattern. Since the shell needs five, the AppleScript string needs ten. (Phew!)

You could type fewer backslashes by using single quotes around the sed pattern (inside single quotes, everything is literal), so then you'd only need three, which means AppleScript needs six:

set someChars to " & ' "
set re to "s|'|\\\\\\&#39;|"
set cmd to "echo " & (quoted form of someChars) & " | sed " & quoted form of re

Gnarlie, your life might be simplified by using perl -p instead of sed. It
Maybe you'd like to try it in the right shell before making that statement. If you paste your command into sh instead of csh, then you get consistent results. Gnarlie, your life would probably be easier if you used Perl instead of sed, since Perl doesn't give "&" any special meaning, and has much better regular expression syntax:

set someChars to " & ' "
set re to "s|'|\\\\&#39;|"
set cmd to "echo " & (quoted form of someChars) & " | perl -pe " & quoted form of re


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: sed replacement headscratcher
      • From: Gnarlodious <email@hidden>
References: 
 >sed replacement headscratcher (From: Gnarlodious <email@hidden>)
 >Re: sed replacement headscratcher (From: Walter Ian Kaye <email@hidden>)

  • Prev by Date: No contextual menu in Script Editor
  • Next by Date: Re: Removing items from a list
  • Previous by thread: Re: sed replacement headscratcher
  • Next by thread: Re: sed replacement headscratcher
  • Index(es):
    • Date
    • Thread