sed 未终止的 `s' 命令 - 从 html 中删除 js 调用 [英] sed unterminated `s' command - removing js call from html

查看:59
本文介绍了sed 未终止的 `s' 命令 - 从 html 中删除 js 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下脚本从 HTML 中删除脚本调用.

I want to remove script calls from the HTML with following script.

var=$(sed  -e '/^<script.*</script>$/d' -e '/.js/!d' testFile.html)

sed -i -e "/$var/d" testFile.html 

示例输入文件:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript</title>
<script type="text/javascript" src="script.js" language="javascript">
</script>

<script>
// script code
</script>
</head>
<body>

</body>
</html>

示例输出文件:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript</title>

</script>

<script>
// script code
</script>
</head>
<body>

</body>
</html>

但是,它给出了以下错误..

But, it gives the following error..

sed: -e expression #1, char 23: unterminated `s' command

提前致谢

推荐答案

目前尚不清楚您为何将其分解为两个单独的脚本,或者您希望变量包含哪些内容.这可以通过单个脚本轻松完成.

It is unclear why you break this up into two separate scripts or what you hope for the variable to contain. This can be performed trivially with a single script.

直接的问题是,如果您使用斜杠作为正则表达式分隔符,则不能在正则表达式中使用文字未转义的斜杠.要么使用不同的分隔符,要么使用反斜杠转义任何文字斜杠.

The immediate problem is that you cannot use a literal unescaped slash in a regex if you use slash as the regex separator. Either use a different separator, or backslash-escape any literal slashes.

sed -i -e '\#^<script.*</script>$#d' -e '/\.js/!d' testFile.html

还要注意点之前的反斜杠(正则表达式中未转义的点匹配任何字符,因此 /.js/ 匹配例如字符串 notjs.)

Notice also the backslash before the dot (an unescaped dot in a regex matches any character, so /.js/ matches e.g. the string notjs.)

这篇关于sed 未终止的 `s' 命令 - 从 html 中删除 js 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆