2012-04-06 14 views
1

코드 스 니펫 (snippet)을 가진 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 형식이 나에게 맞습니다.

진단에 도움이되도록 두 개의 간단한 텍스트 파일과 간단한 패턴으로 시도하십시오.

파일 filewithpattern :

hello 
world 

파일 textinsert :

foo 
goo 

가 이제 나오지도 실행

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} 변수로 바꾸면됩니다. 패턴을 바꿀 수 없기 때문에 하드 코딩 할 수 있지만 스크립트의 맨 위에있는 다른 변수와 변수에 변수를 설정하는 것이 좋습니다. 어떤 var id를 sed param에서 정확하게 평가할 수있는 방법을 알고 싶습니다. – Kurtosis

+0

코드를 게시하면 살펴 보겠습니다. 아마도 그것은 bash 변수 인용 문제 일 것입니다. – joelparkerhenderson

관련 문제