2010-11-21 267 views
1

我试图用sed在AppleScript的shell脚本剥夺这个字符串 - 打印= 1 - 从变量the_html这个网站的链接,这是<a href="http://myurl.com.html?print=1">my link</a>SED和AppleScript shell脚本 - 带字符串

但这会引发错误:

set new_html to do shell script "echo " & quoted form of the_html & " | sed s=?print=1= =g'" 

我需要转义“=”吗?

编辑:

现在工程。 AppleScript的不喜欢的=用\逃脱,但逃脱整个字符串的工作原理:

sed 's/?print=1//g' 

回答

1

试试这个:

echo '<a href="http://myurl.com.html?print=1">my link</a>' | sed 's/?print=1/ /g' 

评论:

  • 加上引号的HTML或逃避它财产
  • 使用sed,您通常使用斜线:“S/A/B/'
1

是。你需要逃避具有特殊含义的角色。现在有标准的正则表达式特殊字符,以及您用作分隔符的字符。因此,如果您使用=作为分隔符,则需要使用\转义它。

通常/用作分隔符。例外的情况是,你可能正在搜索/,这会产生一些非常疯狂和难以阅读的表达式,所有的转义。所以如果你在搜索/ s,我会建议使用不同的角色,否则,坚持/。

要回答这个直接的问题,你可能会逃脱=:

sed '=print\=1= =g'

或使用标准的斜线,没有逃避=:

sed '/print=1/ /g'

1

此工程在我的Mac:

echo '<a href="http://myurl.com.html?print=1">my link</a>' | sed 's=?print\=1= =g' 

所以答案是,是的,你需要逃避=,因为它使用表达式锡永定界符。

+0

谢谢,但我忽略了提醒,我在Applescript中使用shell脚本,并且Applescript不喜欢\以逃避=。 – markratledge 2010-11-21 19:31:42

0

如果使用另一个,则不需要转义分离器,如'/'','。但是,如果你想获得URL 参数

随着'?'前缀:

echo '<a href="http://myurl.com.html?print=1">my link</a>' \ 
    | sed -e 's,.*\(?.*\)\".*,\1,' 

没有'?'

echo '<a href="http://myurl.com.html?print=1">my link</a>' \ 
    | sed -e 's,.*?\(.*\)\".*,\1,' 

最好也分裂参数:

$ echo '<a href="http://myurl.com.html?print=1&convert=4">my link</a>' \ 
    | sed -e 's,.*?\(.*\)\".*,\1,' -e 's,&,\n,g' 
print=1 
convert=4 

有好玩! :)