【问题标题】:How to inline CSS style file to HTML file with sed?如何使用 sed 将 CSS 样式文件内联到 HTML 文件?
【发布时间】:2021-02-21 09:40:54
【问题描述】:

我正在尝试用内联样式标记替换 HTML 标头中的 CSS 文件引用。我正在从文件中搜索以下行。

<link rel="stylesheet" href="style.css" type="text/css">

我想用一行替换上一行

<style>(content of style.css file here)</style>

我有以下 sed 命令用于操作

sed "s/<link rel=\"stylesheet\" href=\"style.css\" type=\"text\/css\">/<style>$(cat style.css)<\/style>/" index.html

但是,sed 不喜欢命令中的 CSS 文件内容并给出错误。

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

没有 style.css 文件的内容,该命令可以正常工作。 如何让这个命令执行我想做的事情,或者是否有替代解决方案来执行相同的操作?

【问题讨论】:

标签: html css bash sed


【解决方案1】:

使用 GNU ;

sed $'/<link rel="stylesheet" href="style.css" type="text\\/css">/{e "<style>$(cat style.css)</style>"\n}' index.html

Try it online! (repl.it)

【讨论】:

  • 无法真正应对这种语法中发生的事情,但sh: 1: 在输出文件中让我感到困惑。
  • 您使用的是什么 SED 版本? GNU?还是你在 MacOs 上?
  • 我在 Ubuntu 18.04 上安装了 GNU sed 4.4。
猜你喜欢
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2010-11-19
  • 2021-10-20
相关资源
最近更新 更多