1

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

我正在尝试使该线程中接受的答案起作用,但事实并非如此,也没有给出错误,所以不知道为什么不:

sed -e '/pattern/r text2insert' filewithpattern

有什么建议么?

模式(在后面插入代码段):

def boot {

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

def\ boot\ {
def\ boot\ \{

文件片段:

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

文件B(引导.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"))

  }
}
4

1 回答 1

4

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

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

文件filewithpattern:

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
于 2012-04-06T23:34:21.610 回答