sed 和 Applescript shell 脚本 - 去除字符串
我正在尝试在 Applescript 的 shell 脚本中使用 sed 从变量 the_html 中的 html 链接中删除此字符串 - ?print=1 - ,即 我的链接
但这会引发错误:
set new_html to do shell script "echo " & quoted form of the_html & " | sed s=?print=1= =g'"
我需要转义“=”吗?
编辑:
现在可以使用。 Applescript 不喜欢用 \
转义 =,但转义整个字符串是可行的:
sed 's/?print=1//g'
I'm trying to use sed in a shell script in Applescript to strip this string - ?print=1 - from this html link in the variable the_html, which is <a href="http://myurl.com.html?print=1">my link</a>
but this throws an error:
set new_html to do shell script "echo " & quoted form of the_html & " | sed s=?print=1= =g'"
Do I need to escape the "="?
Edit:
Works now. Applescript didn't like an = escaped with a \
, but escaping the whole string works:
sed 's/?print=1//g'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
注释:
Try this:
Comments:
sed
, you generally use slashes: 's/a/b/'这在我的 Mac 上有效:
所以答案是,是的,您确实需要转义
=
因为它用作表达式分隔符。This works on my Mac:
So the answer is, yes, you do need to escape the
=
since it is used an expression delimiter.是的。您需要转义具有特殊含义的字符。现在有标准正则表达式特殊字符以及用作分隔符的字符。因此,如果您使用 = 作为分隔符,则需要使用 \ 对其进行转义。
通常使用/作为分隔符。例外是当您搜索 / 时,这会产生一些非常疯狂且难以阅读的带有所有转义的表达式。因此,如果您正在搜索 /s,我建议使用不同的字符,但否则请坚持使用 /。
要回答眼前的问题,您可以逃避=:
或使用标准斜杠,而不转义 =:
Yes. You need to escape the characters that have a special meaning. Now there are the standard regular expression special characters, and the character that you are using as a delimiter. So if you use = as the delimiter, you'll need to escape it with \.
Usually / is used as the delimiter. The exception is when you might be searching for /, which yields some pretty crazy and hard to read expressions with all the escaping. So if you're searching for /s, I'd suggest using a different character, but otherwise, stick to /.
To answer the immediate question, you might escape the =:
or use the standard slash, without escaping the =:
如果使用其他
sed
分隔符(如'/'
或','
),则不需要转义'='
>。但如果你想获取 URL 参数:带
'?'
前缀:不带
'?'
:最好也是拆分参数:
玩得开心 ! :)
You don't need to escape
'='
if using anothersed
separator like'/'
or','
. But if you wish to get URL parameters:With
'?'
prefix:Without the
'?'
:The best is also to split parameters:
Have fun ! :)