【问题标题】:How can I remove html tags, scripts, empty lines in a HTML with sed?如何使用 sed 删除 HTML 中的 html 标签、脚本、空行?
【发布时间】:2020-06-19 12:50:25
【问题描述】:

我尝试了一些方法,但我无法做到。

sed -e "s/<!---* *<\(.*\)> *-->/<\1>/" test.html

我在这里找到了,但我不知道如何继续。

【问题讨论】:

标签: html linux bash sed


【解决方案1】:

我尝试了以下脚本来删除 html / 标签输出剩下的内容

输入文件:(test.txt)

<!DOCTYPE html>

  <head>

    <script>
      function myFunction() {
        document.getElementById("demo").innerHTML = "Paragraph changed.";
      }
    </script>

    <title>Sample page</title>

    <style>
      html { color: #837456; }
      body { background: white; }
    </style>

  </head>

  <body>
    <p>Hello here!</p>
  </body>

</html>

命令是

cat test.txt | sed 's/$/©/' | tr -d '\n' | sed 's/<script.*<\/script>//g' | sed 's/<[^>]*>//g' | sed 's/©/\n/g' | sed '/^ *$/d' | sed 's/^ *//'

输出恰好是:

Sample page
html { color: #837456; }
body { background: white; }
Hello here!

【讨论】:

    猜你喜欢
    • 2017-02-27
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    相关资源
    最近更新 更多