【问题标题】:Bash script to insert code from one file at a specific location in another file?Bash脚本将一个文件中的代码插入另一个文件的特定位置?
【发布时间】:2012-04-20 11:24:51
【问题描述】:

我有一个带有sn-p 代码的fileA,我需要一个脚本将该sn-p 插入到fileB 中特定模式之后的行中。

我正在尝试使 accepted answer in this thread 工作,但它没有,也没有给出错误,所以不知道为什么不:

sed -e '/pattern/r text2insert' filewithpattern

有什么建议吗?

pattern (插入 sn-p on line after):

def boot {

也尝试了转义模式,但没有运气:

def\ boot\ {
def\ boot\ \{

fileA sn-p:

    LiftRules.htmlProperties.default.set((r: Req) =>
        new Html5Properties(r.userAgent))

fileB (Boot.scala):

package bootstrap.liftweb
import net.liftweb._
import util._
import Helpers._
import common._
import http._
import sitemap._
import Loc._


/**
 * A class that's instantiated early and run.  It allows the application
 * to modify lift's environment
 */
class Boot {
  def boot {
    // where to search snippet
    LiftRules.addToPackages("code")

    // Build SiteMap
    val entries = List(
      Menu.i("Home") / "index", // the simple way to declare a menu

      // more complex because this menu allows anything in the
      // /static path to be visible
      Menu(Loc("Static", Link(List("static"), true, "/static/index"), 
           "Static Content")))

    // set the sitemap.  Note if you don't want access control for
    // each page, just comment this line out.
    LiftRules.setSiteMap(SiteMap(entries:_*))

    // Use jQuery 1.4
    LiftRules.jsArtifacts = net.liftweb.http.js.jquery.JQuery14Artifacts

    //Show the spinny image when an Ajax call starts
    LiftRules.ajaxStart =
      Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)

    // Make the spinny image go away when it ends
    LiftRules.ajaxEnd =
      Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

    // Force the request to be UTF-8
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))

  }
}

【问题讨论】:

    标签: bash shell scala scripting sed


    【解决方案1】:

    sed 格式对我来说是正确的。

    为了帮助您诊断此问题,请尝试使用两个更简单的文本文件和一个简单的模式。

    文件文件与模式:

    hello
    world
    

    文件文本插入:

    foo
    goo
    

    现在运行 sed:

    sed -e '/hello/r textinsert' filewithpattern
    

    你应该看到这个:

    hello
    foo
    goo
    world
    

    这对你有用吗?

    如果是这样,则编辑 filewithpattern 以使用您的目标:

    hello
    def boot {
    world
    

    运行命令:

    sed -e '/def boot {/r textinsert' filewithpattern
    

    你应该看到这个:

    hello
    def boot {
    foo
    goo
    world
    

    如果你想要变量替换,试试这个:

    #!/bin/bash
    PATTERN='def boot {'
    sed -e "/${PATTERN}/r textinsert" filewithpattern
    

    【讨论】:

    • 谢谢,我也确实如此。去建立filewithpattern看看问题出在哪里。
    • 当我从 sed 命令中的硬编码“def boot {”切换到用变量 $PATTERN 或 ${PATTERN} 替换它时,就会发生这种情况。我可以对它进行硬编码,因为模式不会改变,但是最好将它设置在脚本顶部的变量中,并与其他变量一起使用。知道如何在 sed 参数中正确评估该 var 吗?
    • 发布您的代码,我会看看。可能是 bash 变量引用问题。
    • 我在答案中附加了一个变量 PATTERN 的例子——它对你有用吗?
    • 啊哈! sed 字符串周围的双引号是诀窍。我在那里使用单引号。现在一切正常,包括原始的完整脚本!非常感谢,我不知道要花多长时间才能弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多