1

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

  }
}
Community
  • 1
  • 1
bgibson
  • 17,379
  • 8
  • 29
  • 45

1 Answers1

5

The sed format looks correct to me.

To help you diagnose this, try it with two simpler text files and a simple pattern.

File filewithpattern:

hello
world

File textinsert:

foo
goo

Now run sed:

sed -e '/hello/r textinsert' filewithpattern

You should see this:

hello
foo
goo
world

Does that work for you?

If so, then edit filewithpattern to use your target:

hello
def boot {
world

Run the command:

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

You should see this:

hello
def boot {
foo
goo
world

If you want variable substitution, try this:

#!/bin/bash
PATTERN='def boot {'
sed -e "/${PATTERN}/r textinsert" filewithpattern
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
  • Thanks, that does indeed for me too. Going to build up filewithpattern to see where the problem is. – bgibson Apr 07 '12 at 00:49
  • It's happening when I switch from hard coding 'def boot {' in the sed command, to replacing it with a variable $PATTERN or ${PATTERN}. I could hard code it since the pattern won't change, but would be nice to set it in a variable at the top of my script with the other vars. Any idea how to get that var to evaluate correctly in the sed param? – bgibson Apr 07 '12 at 01:03
  • Post your code and I'll take a look. Likely it's a bash variable quoting issue. – joelparkerhenderson Apr 07 '12 at 01:03
  • I appended the answer with an example of variable PATTERN -- does it work for you? – joelparkerhenderson Apr 07 '12 at 01:28
  • Ah hah! Double quotes around the sed string is the trick. I was using single quotes there. Everything works now, including the original full script! Thank you soo much, I don't know how long it would have taken to figure that out. – bgibson Apr 07 '12 at 01:57