1

コードのスニペットを含むfileAがあり、そのスニペットを特定のパターンの後の行のfileBに挿入するスクリプトが必要です。

私はこのスレッドで受け入れられた答えを機能させようとしていますが、そうではなく、エラーを出していないので、なぜそうではないのかわかりません:

sed -e '/pattern/r text2insert' filewithpattern

助言がありますか?

パターン(後の行にスニペットを挿入):

def boot {

エスケープされたパターンも試しましたが、運がありませんでした。

def\ boot\ {
def\ boot\ \{

fileAスニペット:

    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"))

  }
}
4

1 に答える 1

4

sedの形式は私には正しいように見えます。

これを診断するのに役立つように、2つの単純なテキストファイルと単純なパターンで試してみてください。

パターン付きファイルfile:

hello
world

ファイルtextinsert:

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 に答える