【问题标题】:sed replace and add multiple lines with html codesed 用 html 代码替换和添加多行
【发布时间】:2020-04-28 16:10:06
【问题描述】:

我一直在尝试各种解决方案,但似乎没有一个对我有用,所以是时候打电话给专家了。

我有一个 index.html 文件,该文件将使用 bash 脚本进行更新,以添加新的 HTML 代码。 bash 脚本将使用变量作为 sed 行的一部分。我希望它找到以下行

<!--insert_new_code-->

然后用

替换该行
  <div class="col-lg-4 col-md-6">
    <div w3-include-html="explorers/$VARIABLE.html"></div>
  </div>

    <!--insert_new_code-->

所以如果我运行 sed 命令 3 次,它看起来像这样

      <div class="col-lg-4 col-md-6">
        <div w3-include-html="explorers/1.html"></div>
      </div>

      <div class="col-lg-4 col-md-6">
        <div w3-include-html="explorers/2.html"></div>
      </div>

      <div class="col-lg-4 col-md-6">
        <div w3-include-html="explorers/3.html"></div>
      </div>

   <!--insert_new_code-->

找到了解决方案。

sed -i -e '/<!--insert_new_code-->/i\          <div class="col-lg-4 col-md-6">\n            <div w3-include-html="explorers/'"${coin_name}"'.html">\n          </div>\n\n' index.html

【问题讨论】:

  • 嗨,欢迎来到 Stackoverflow。很高兴您说明了您需要完成的结果。很好解释。我猜人们会想帮助你解决这个问题,但为了让这成为可能,你写了哪些 BASH 代码没有做你需要做的事情?你有什么问题?
  • 您好,刚刚找到我的解决方案并发布在上面。

标签: bash sed


【解决方案1】:

使用sed 脚本:

sed -e '/insert_new_code/i\<div class="col-lg-4 col-md-6">\
        <div w3-include-html="explorers/1.html"></div>\
      </div>\

' index.html 

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 2021-05-19
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多