Bash script to insert code from one file at a specific location in another file?

Posted by Kurtosis on Stack Overflow See other posts from Stack Overflow or by Kurtosis
Published on 2012-04-06T23:24:05Z Indexed on 2012/04/06 23:30 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

I have a fileA with a snippet of code, and I need a script to insert that snippet into fileB on the line after a specific pattern.

I'm trying to make the accepted answer in this thread work, but it's not, and is not giving an error so not sure why not:

sed -e '/pattern/r text2insert' filewithpattern

Any suggestions?

pattern (insert snippet on line after):

def boot {

also tried escaped pattern but no luck:

def\ boot\ {
def\ boot\ \{

fileA snippet:

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

  }
}

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell