我有一个带有代码片段的fileA,我需要一个脚本,在特定模式之后将该代码段插入到行中的fileB中。
我正在努力使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"))
}
}
答案 0 :(得分:3)
sed格式对我来说是正确的。
为了帮助您诊断,请尝试使用两个更简单的文本文件和一个简单的模式。
文件filewithpattern:
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