I wanna insert new line with indent router.Use(apihandlers.AuthMiddleWare)
after the specific word router := gin.Default()
.
func NewRouter() *gin.Engine {
router := gin.Default()
// I wanna insert line here.(with indent)
//router.Use(apihandlers.AuthMiddleWare)
for _, route := range RouteSettings {
switch route.Method {
.
.
.
}
I try to implement it by sed command.
But It does not work now.
Maybe my Regular Expression is wrong.
sed -e '/^router := gin.Default()$/a router.Use(apihandlers.AuthMiddleWare)' {PATH_TO_THE_FILE}/routers.go
So please give me the correct Regular Expression.
Thanks in advance.
CodePudding user response:
I am not sure you can do it with a
command if you want it to automatically use proper indent, but you can try with s
. Here we capture both the indent and the rest of the line and replace it with:
- itself:
\1\2
- new line:
\n
- captured indent:
\1
- new content:
router...
sed -e 's/^\([[:space:]]*\)\(router := gin.Default()\)$/\1\2\n\1router.Use(apihandlers.AuthMiddleWare)/'
CodePudding user response:
This might work for you (GNU sed):
sed '/router := gin\.Default()/{p;s/\S.*/line appended and indented/}' file
Match on the required line.
Print the required line.
Using the required line as a template, insert another line by overwriting the template line.