sed从缩小的html中删除javascript调用

时间:2018-07-17 13:12:43

标签: javascript sed

我想从缩小的html文件中删除所有外部js调用。但是,如果js调用位于另一行中,则以下内容将删除(不适用于缩小的html)

var=$(sed  -e '/^<script.*<\/script>$/d' -e '/.js/!d' testFile.html)
grep -v "$var" "testFile.html" | sponge "testFile.html"

样本输入文件:

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

示例输出文件:

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

预先感谢

1 个答案:

答案 0 :(得分:0)

不幸的是,sed不支持非贪婪/惰性匹配。试试perl:

 perl -pe 's/<script.*?script> //'