Necesito y Applescript para reemplazar un texto de la siguiente manera:
Texto original:
string string string $ text1
string string string $ text2
text3
string string string $ text4
La salida requerida es:
$ text1
$ text2
text3
$ text4
Puedo hacerlo en la terminal con este comando:
$ echo "string string string $ text1
string string string $ text2
text3
string string string $ text4" | sed -r 's/^(.*)\$ ?(.) (.*)$/$ \3/g'
$ text1
$ text2
text3
$ text4
Por cierto, estoy usando bash versión 4.3.30 y sed 4.2.2, ambos de homebrew.
El problema aquí es que necesito hacerlo desde un applescript. Este es mi enfoque:
set commandString to "echo \"string string string $ text\" | sed -r 's|^(.*)\\$ ?(.) (.*)$|$ \\3|g'" as string
set formattedCode to do shell script commandString
Y me sale el siguiente 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
Si elimino la -r
opción, obtengo un error diferente:
sed: 1: "s|^(.*)\$ ?(.) (.*)$|$ ...": \3 not defined in the RE
Si elimino el \3
, la salida debe ser $
en lugar de $ text
, pero sed
el comando no hace nada y genera:
string string string $ text
Supuse que esto podría ser un problema con sed
la versión. Entonces, si lo reemplazo sed
con /usr/local/bin/sed
él, no vuelve a hacer nada después de la línea set formattedCode to do shell script commandString
.
Alguien sabe donde esta el problema?
La opción -r de GNU sed es -E en OS X/BSD sed (la que viene con el sistema operativo /usr/bin/sed
). Y para deshacerse del problema de codificación con , agregue export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8;
al principio del comando do shell script (vea la pregunta aquí ):
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
Devoluciones:
$ text1
$ text2
text3
$ text4
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
Devoluciones:
$ text1
$ text2
text3
$ text4
-E
funcione en la terminal, pero no en el applescript.
markhunte