Hi.
If I’ve correctly understood the object of the code:
do shell script "replace='Laurel & Hardy; PS\\2' # sample input containing metachars.
replaceEscaped=$(sed 's/[&/\\]/\\\\&/g' <<<\"$replace\") # escape it
sed -n \"s/\\(.*\\) \\(.*\\)/$replaceEscaped/p\" <<<\"foo bar\" # if ok, outputs $replace as is"
The reason the sed string in the last line is in double quotes rather than single quotes is to make the shell insert the contents of the variable replaceEscaped rather than the text “$replaceEscaped”. Alternatively, you could use single quotes, but interrupt them with a double-quoted section (a bit like inserting a variable into a string in AppleScript):
do shell script "replace='Laurel & Hardy; PS\\2' # sample input containing metachars.
replaceEscaped=$(sed 's/[&/\\]/\\\\&/g' <<<\"$replace\") # escape it
sed -n 's/\\(.*\\) \\(.*\\)/'\"$replaceEscaped\"'/p' <<<\"foo bar\" # if ok, outputs $replace as is"
There are various ways to reduce the “wicker fence” effect. For instance, when the Mac sed’s -E option’s used, “(”, “)”, and “|” are understood to be metacharacters and don’t need to be escaped. (It’s the literal equivalents which have to be escaped then.)
do shell script "replace='Laurel & Hardy; PS\\2' # sample input containing metachars.
replaceEscaped=$(sed 's/[&/\\]/\\\\&/g' <<<\"$replace\") # escape it
sed -En 's/(.*) (.*)/'\"$replaceEscaped\"'/p' <<<\"foo bar\" # if ok, outputs $replace as is"
. not to mention the fact that the parentheses in the last line aren’t needed anyway. Also, the delimiter in sed’s ‘s’ command doesn’t have to be a slash. Whatever character comes after the ‘s’ (with a few exceptions) is taken as the delimiter:
do shell script "replace='Laurel & Hardy; PS\\2' # sample input containing metachars.
replaceEscaped=$(sed 's:[&/\\]:\\\\&:g' <<<\"$replace\") # escape it
sed -n 's:.* .*:'\"$replaceEscaped\"':p' <<<\"foo bar\" # if ok, outputs $replace as is"
That’s not a particularly efficient regex in the last line, but it serves for the demo.
Edit: A bit more about the AppleScript text:
The double quotes at either end signify that it’s text and aren’t in the text itself.
The text contains double-quote and backslash characters and each of these needs to be escaped with a backslash when the text is typed into Script Editor. The escaping backslashes are also not actually in the text but are just there to help it compile. If you replace ‘do shell script’ with ‘display dialog’ in the first script above, the text displayed is the actual shell code.
The shell code itself contains some escaping, also involving backslashes. Each of these backslashes must be represented by two in the AppleScript text (as seen in Script Editor). The most extreme example is the “\\&” in the second line. There, the “&” is a metacharacter representing the text matched by the “[&/\]” regex. The aim is to insert a backslash character in front of the matched text. This backslash has to be written as two backslashes in the sed command in order to get a single backslash character in the text being edited. Both sed backslashes have to be escaped when representing the code inside an AppleScript string in Script Editor. Hence four backslashes are needed to get two in sed to get one actual backslash in the text which eventually winds up as an escape character itself in replaceEscaped!