首页 > 解决方案 > 如何用shell脚本替换html文件中的多行文本?

问题描述

我有一个 html 文件,我想在其中使用 shell 脚本使用函数替换样式sed。我想更换:

    pre {
      font-size: inherit;
      line-height: inherit;
    }

和:

    pre {
      font-size: 18px;
      /*line-height: inherit;*/
    }

我尝试了以下方法:

    sed -i 's/pre {\n   font-size: inherit;\n   line-height: 
    inherit;\n}/pre {\n font-size: 18px;\n  \/\*line-height: 
    inherit;\*\/\n}/g' /path/file.html

它运行没有任何错误,但不会替换文本。请注意,我包含用于微调文本模式的选项卡。我正在开发 ubuntu 18。

任何建议将不胜感激。

标签: htmlbashcssshell

解决方案


您可以在 sed 中使用匹配的模式引用,例如:

sed -i 's/line-height: inherit;/\/*&*\//g' /path/file.html

  • 在这里,我们使用line-height: inherit;匹配搜索模式。您也可以将其扩展为正则表达式

  • &用于引用匹配的文本,我们将\/*and*\/作为封闭字符


推荐阅读