2012-04-06 14 views
1

コードスニペットを持つfileAがあります。特定のパターンの後に、そのスニペットをfileBの行に挿入するスクリプトが必要です。あるファイルから別のファイルの特定の場所にコードを挿入するBashスクリプト?

私はaccepted answer in this thread仕事をしようとしてんだけど、そうではありませんし、そうわからないエラーが示されるのはなぜいないされていません。

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

    } 
} 

答えて

3

sed形式が正しいと思われます。

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

ファイルfilewithpattern:

hello 
world 

ファイルtextinsert:

foo 
goo 

今すぐsedを実行します。

hello 
foo 
goo 
world 
0:

sed -e '/hello/r textinsert' filewithpattern 

あなたはこれを見るべきです

それはあなたのために機能しますか?

hello 
def boot { 
world 

次のコマンドを実行します:

もしそうなら、あなたのターゲットを使用するfilewithpattern編集

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

あなたが表示されるはずです。この:あなたは変数置換をしたい場合は

hello 
def boot { 
foo 
goo 
world 

試してみてください:

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

本当にありがとうございます。問題がどこにあるかを見るためにfilewithpatternをビルドしようとしています。 – Kurtosis

+0

sedコマンドのdef boot {'をハードコーディングから変数$ PATTERNまたは$ {PATTERN}に置き換えることに変わりました。パターンは変更されないのでハードコードすることができますが、スクリプトの上部にある変数に他の変数を設定するといいでしょう。任意のアイデアはどのようにsedのparamで正しく評価するvarを取得するには? – Kurtosis

+0

コードを投稿すると、私は見ていきます。おそらく、これはbash変数の引用問題です。 – joelparkerhenderson

関連する問題